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

标签 php mysql multidimensional-array

我希望一周的学生和事件以日历格式显示。我有如下数组。每个学生有 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>

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

最佳答案

<?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>

关于php - 使用多维数组构建具有 rowspan 的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880559/

相关文章:

php - 如何检索旧的 Whatsapp 个人资料图片?

php - 如何使用 RapidShare API?

mysql - 查询查找每年销售额超过平均销售额的销售员列表

python - 通过添加列来更新 NumPy 数组

C# 错误消息 : Cannot Implicitly convert type

php - PayPal-PHP-SDK 是否支持向另一个 paypal 帐户付款?

php - 在 mysql 中的 4 个表的子查询中使用外部别名

php - 从 xml 变量中过滤掉撇号

php - MySQLi输入数据到数据库

c - 多维数组的别名