使用多维数组构建具有rowspan的表

0wi1tuuw  于 2021-06-15  发布在  Mysql
关注(0)|答案(3)|浏览(323)

我希望本周的学生和事件以日历格式显示。每个学生从星期一到星期天有7个数组,每个数组的内部数组有一天的事件

$array = [
    'Alex' => [
        [
            ['event' => 'eventName1'],['event' => 'eventName2']
        ],
        [
            ['event' => 'eventName3'],['event' => 'eventName4']
        ],
        [
            ['event' => 'eventName5'],['event' => 'eventName6']
        ],
        [
            ['event' => 'eventName7'],['event' => 'eventName8']
        ],
        [],
        [],
        []

    ], 
    'Allen' => [
        [
            ['event' => 'eventName'],['event' => 'eventName']   
        ],[
            ['event' => 'eventName'],['event' => 'eventName']  
        ],
        [],
        [],
        [],
        [],
        []
    ],
];

我需要数组显示为周日历,我不知道下面的代码哪里出错了

<table>
    <thead>
        <tr>
            <th>Participant</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thrusday</th>
            <th>Friday</th>
            <th>Satday</th>
            <th>Sunday</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($array as $participant => $event): ?>
        <?php foreach ($event as $i => $value):?>
        <tr>
            <?php if ($i === 0): ?>
            <td rowspan="2"><?= $participant ?></td>
            <?php endif ?>
            <?php foreach ($value as $index  => $eventD):?>

            <td><?php echo $eventD['event']; ?></td>

        </tr>
        <?php endforeach ?>
        <?php endforeach ?>
        <?php endforeach ?>
    </tbody>
</table>

我的问题是循环以及如何打印特定日期的事件。任何人都可以在建立正确的格式。谢谢你的帮助

czq61nw1

czq61nw11#

如果我的理解是正确的,就这样试试

<?php

  $array = [
'Alex' => [
    [
        ['event' => 'eventName1'],['event' => 'eventName2']
    ],
    [
        ['event' => 'eventName3'],['event' => 'eventName4']
    ],
    [
        ['event' => 'eventName5'],['event' => 'eventName6']
    ],
    [
        ['event' => 'eventName7'],['event' => 'eventName8']
    ],
    [],
    [],
    []

 ], 
'Allen' => [
    [
        ['event' => 'eventName'],['event' => 'eventName']   
    ],[
        ['event' => 'eventName'],['event' => 'eventName']  
    ],
    [],
    [],
    [],
    [],
    []
   ],
];

    echo "<table border='1'>
 <thead>
    <tr>
        <th>Participant</th>
        <th>Monday</th>
        <th>Tuesday</th>
        <th>Wednesday</th>
        <th>Thrusday</th>
        <th>Friday</th>
        <th>Satday</th>
        <th>Sunday</th>
    </tr>
</thead>
<tbody>";
     foreach($array as $key => $data)
      {
    echo "<tr>
    <td>$key</td>";
    foreach($data as $data2) {
        if(is_array($data2) && isset($data2[0])) {
            echo "<td>";
             array_walk($data2, function($val, &$dat){
                echo $val['event'].', ';
            });
            echo "</td>";
        } else {
            echo "<td></td>";
        }
    }
    echo "</tr>";
}
   echo "</tbody>
    </table>";
     ?>

小提琴连杆http://sandbox.onlinephpfunctions.com/

chy5wohz

chy5wohz2#

检查此html

<table border="1">
    <thead>
        <tr>
            <th>Participant</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thrusday</th>
            <th>Friday</th>
            <th>Satday</th>
            <th>Sunday</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($array as $participant => $event): ?>
            <tr>
                <td><?= $participant ?></td>
                <?php foreach ($event as $i => $value): ?>
                    <td>
                        <?php foreach ($value as $index => $eventD): ?>
                            <?php echo $eventD['event'] . '<br/>'; ?>
                        <?php endforeach ?>
                    </td>
                <?php endforeach ?>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>
webghufk

webghufk3#

<?php

$array = [
    'Alex' => [
        [
            ['event' => 'eventName1'],['event' => 'eventName2']
        ],
        [
            ['event' => 'eventName3'],['event' => 'eventName4']
        ],
        [
            ['event' => 'eventName5'],['event' => 'eventName6']
        ],
        [
            ['event' => 'eventName7'],['event' => 'eventName8']
        ],
        [],
        [],
        []
    ], 
    'Allen' => [
        [
            ['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],   
        ],[
            ['event' => 'eventName'],['event' => 'eventName']  
        ],
        [],
        [],
        [],
        [],
        []
    ],
];

$maxRows = [];
foreach ($array as $participant => $weekEvents) {
    $max = 0;
    foreach ($weekEvents as $dayEvents) {
        if (count($dayEvents) > $max) {
            $max = count($dayEvents);
        }
    }

    $maxRows[$participant] = $max;
}

?>

<table border="1">
    <thead>
        <tr>
            <th>Participant</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thrusday</th>
            <th>Friday</th>
            <th>Satday</th>
            <th>Sunday</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($array as $participant => $weeklyEvents): ?>
            <?php for ($i = 0; $i < $maxRows[$participant]; $i++): ?>
                <tr>
                    <?php if ($i === 0): ?>
                        <td rowspan="<?php echo $maxRows[$participant]; ?>">
                            <?php echo $participant; ?>
                        </td>
                    <?php endif; ?>

                    <?php for ($j = 0; $j < 7; $j++): ?>
                        <td>
                            <?php $dayEvents = $weeklyEvents[$j]; ?>

                            <?php if (isset($dayEvents[$i])): ?>
                                <?php echo $dayEvents[$i]['event']; ?>
                            <?php else: ?>
                                &nbsp;
                            <?php endif; ?>
                        </td>
                    <?php endfor; ?>
                </tr>
            <?php endfor; ?>
        <?php endforeach; ?>
    </tbody>
</table>

相关问题