PHP - 使用2D数组创建HTML表

时间:2016-02-19 16:52:24

标签: php arrays

我有一个像bellow这样的数组,数组中的数据有点大,

Array ( [0] => stdClass Object ( [ID] => 1 [cyear] => 2016 [cmonth] => 1 [cmonthname] => January [cday] => 1 [cdayname] => Friday [ctime] => 9:00 [status] => 6 [notes] => )
        [1] => stdClass Object ( [ID] => 2 [cyear] => 2016 [cmonth] => 1 [cmonthname] => January [cday] => 1 [cdayname] => Friday [ctime] => 9:15 [status] => 1 [notes] => )
        [2] => stdClass Object ( [ID] => 3 [cyear] => 2016 [cmonth] => 1 [cmonthname] => January [cday] => 1 [cdayname] => Friday [ctime] => 9:30 [status] => 1 [notes] => )
        [3] => stdClass Object ( [ID] => 4 [cyear] => 2016 [cmonth] => 1 [cmonthname] => January [cday] => 1 [cdayname] => Friday [ctime] => 9:45 [status] => 1 [notes] => )
        [4] => stdClass Object ( [ID] => 5 [cyear] => 2016 [cmonth] => 1 [cmonthname] => January [cday] => 1 [cdayname] => Friday [ctime] => 10:00 [status] => 1 [notes] => )
        [5] => stdClass Object ( [ID] => 6 [cyear] => 2016 [cmonth] => 1 [cmonthname] => January [cday] => 1 [cdayname] => Friday [ctime] => 10:15 [status] => 1 [notes] => )

这背后的想法,

每个数组表示从上午9点到下午5点的给定时隙(15分钟)

这意味着一小时有04个数组/ stdClass对象(9:00,9:15,9:9,9:45)

一天有32个数组/ stdClass对象9.00AM到5.00PM - > 8小时 所以8 * 4 = 32

因此,对于2016年2月,有928(32 * 29)个数组/ stdClass对象 你可以

download the full data array here.

使用这个我想制作如下的HTML表格,

enter image description here

绿行是天(2016年1月29日,2月)

黄色栏目是时段(上午9点到下午4点45分)

1s是每个数组/ stdClass对象的状态

1 个答案:

答案 0 :(得分:1)

当您可以水平打印然后垂直打印时,HTML往往更容易打印,所以首先将其放入网格中。

$newArray=[][];
foreach($hugeArray as $block)
    $newArray[$block["ctime"]][$block["cday"]]=$block["status"];

现在你可以双循环打印。 (假设使用表并排除第一行):

foreach($newArray as $ctime=>$smalArray)
{
    print("<tr><td>".$ctime."</td>");
    foreach($smalArray as $cday => $status)
        print("<td>".$status."</td>");
    print("</tr>");
}