我希望每周的学生和活动以日历格式显示。我有以下数组。每个学生从周一到周日都有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>
我的问题是循环以及如何打印特定日期的事件。 任何人都可以建立正确的格式。感谢您的帮助
答案 0 :(得分:0)
检查此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>
答案 1 :(得分:0)
<?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: ?>
<?php endif; ?>
</td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
<?php endforeach; ?>
</tbody>
</table>
答案 2 :(得分:0)
如果我的理解正确,请尝试这样
<?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>";
?>