PHP-使用2D数组生成HTML表
我有一个像bellow这样的数组,数组中的数据有点大PHP-使用2D数组生成HTML表,php,arrays,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] => st
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:00到下午5:00的给定时间段(15分钟)
这意味着一小时内有04个数组/stdClass对象(9:00、9:15、9:30、9:45)
一天有32个数组/stdClass对象,从上午9点到下午5点->8小时
所以8*4=32
因此,2016年2月,共有928(32*29)个阵列/stdClass对象
你可以
我想用它制作一个HTML表格,如下所示
绿行是指日期(2016年2月1日至29日)
黄色列为时隙(上午9:00至下午4:45)
1是每个数组/stdClass对象的状态当您可以水平打印,然后垂直打印时,HTML往往更容易打印,因此首先将其放入网格中
$newArray=[][];
foreach($hugeArray as $block)
$newArray[$block["ctime"]][$block["cday"]]=$block["status"];
现在您可以双循环打印。(假设使用表格,不包括第一行):
foreach($ctime=>newArray作为$smalArray)
{
打印(“$ctime.”);
foreach($smalArray作为$cday=>$status)
打印(“$status.”);
打印(“”);
}
我是不是遗漏了一个问题?我不明白这个问题。你要什么?如何打印表格?是的,我提到过“我想制作一个如下所示的HTML表格”。无论如何,是的,我想使用arrayIt在图像中制作表格。似乎有一种更简单(且对服务器友好)的方法,可以使用php time()、date()和strotime()函数以及while循环来实现这一点。任何注释或状态都可以存储在带有时间戳的mysql表中。谢谢你。谢谢你的帮忙。您的算法正在运行:))
foreach($newArray as $ctime=>$smalArray)
{
print("<tr><td>".$ctime."</td>");
foreach($smalArray as $cday => $status)
print("<td>".$status."</td>");
print("</tr>");
}