像下面这样的东西会起作用。我猜到了您拥有的数据类型,但基本上您只需找到项目最多的th
一天,为每一天创建一个tr
,为最大一天中的尽可能多的项目创建一个,最后td
为每一天创建一个打印对应于 for 循环计数器的项目。
<?php
$mon_arr = array("mon 1", "mon 2", "mon 3");
$tue_arr = array("tue 1", "tue 2", "tue 3", "tue 4");
$wed_arr = array("wed 1", "wed 2", "wed 3", "wed 4");
$thu_arr = array("thu 1", "thu 2");
$fri_arr = array("fri 1", "fri 2", "fri 3", "fri 4", "fri 5");
$sat_arr = array("sat 1");
$sun_arr = array("sun 1");
$week = array(
"Monday" => $mon_arr,
"Tuesday" => $tue_arr,
"Wednesday" => $wed_arr,
"Thursday" => $thu_arr,
"Friday" => $fri_arr,
"Saturday" => $sat_arr,
"Sunday" => $sun_arr
);
$max_day = 0;
foreach($week as $day => $data){
$current = count($data);
if($current > $max_day){
$max_day = $current;
}
}
?>
<table>
<thead>
<tr>
<?php
foreach( $week as $day => $data ){
echo "<th>$day</th>";
}
?>
</tr>
</thead>
<tbody>
<?php for( $i = 0; $i < $max_day; $i++ ){ ?>
<tr>
<?php
foreach( $week as $day => $data ){
if( $data[$i] ){
echo "<td>$data[$i]</td>";
} else {
echo "<td> </td>";
}
}
?>
</tr>
<?php } ?>
</tbody>
</table>