【发布时间】:2013-02-07 07:58:50
【问题描述】:
我的数组 -
$myfinal = Array(
13 => Array
(
5 => 85,
4 => 75,
3 => 65,
2 => 55
),
12 => 11,
7 => 100
);
这就是我想要动态生成的(表)-
所需输出 - http://jsfiddle.net/LCKW6/
<table cellspacing="1" cellpadding="4" border="3" bgcolor="#f5f5f5">
<tbody>
<tr bgcolor="#99cccc">
<th colspan="4">13</th>
<th colspan="0">12</th>
<th colspan="0">7</th>
</tr>
<tr bgcolor="#99cccc">
<th width="70">5</th>
<th width="70">4</th>
<th width="70">3</th>
<th width="70">2</th>
<th width="70">No subcat</th>
<th width="70">No subcat</th>
</tr>
<tr align="right">
<td>85</td>
<td>75</td>
<td>65</td>
<td>55</td>
<td>11</td>
<td>100</td>
</tr>
</tbody></table>
我的代码尝试,我尝试了第一个 tr 和 th,但其余的我对循环感到困惑:
<?php
$myfinal = Array(
13 => Array
(
5 => 85,
4 => 75,
3 => 65,
2 => 55
),
12 => 11,
7 => 100
);
?>
<table cellspacing="1" cellpadding="4" border="3" bgcolor="#c3cece">
<tbody>
<tr bgcolor="#99cccc">
<?php
foreach( $myfinal as $key => $value )
{
if( is_array($value) )
{
echo '<th colspan="'.sizeof($value).'">'.$key.'</th>';
}
else
{
echo '<th colspan="0">'.$key.'</th>';
}
}
?>
</tr>
</tbody>
</table>
【问题讨论】:
-
你需要
colspan = "1",而不是0 -
您需要任意深度还是只需 2 层?
-
@PhilipWhitehouse 这种类型的输出我需要jsfiddle.net/LCKW6
-
@PhilipWhitehouse 不,实际上我只需要像这样设置表格标题..它都将是动态的..因为较低的 trs 将从另一个表格中填充
标签: php arrays loops logic html-table