我在将以下数组显示到表中时有问题。这个想法是基于第一个数组水平显示数据,但是第二个数组也应该被访问并水平显示。
数组(为了显示,我把数组颠倒了):
Array (
[3] => Array (
[name] => Joop
[details] => Array (
[length] => 184
[weight] => 86
...
)
[year] => 1984
[university] => Harvard
[class_id] => 37
[group_id] => 14
)
[2] => Array (
[name] => Tim
[details] => Array (
[length] => 169
[weight] => 74
...
)
[year] => 1977
[university] => MIT
[class_id] => 37
[group_id] => 14
)
[1] => Array (
[name] => Kelly
[details] => Array (
[length] => 164
[weight] => 51
...
)
[year] => 1982
[university] => MIT
[class_id] => 37
[group_id] => 14
)
[0] => Array (
[name] => Alex
[details] => Array (
[lenght] => 172
[weight] => 79
...
)
[year] => 1979
[university] => Harvard
[class_id] => 37
[group_id] => 14
)
我希望在HTML表格中显示它,如下所示:
Name | Joop | Tim | Kelly | Alex
Length | 184 | 169 | 164 | 172
weight | 86 | 74 | 51 | 79
...
...
Year | 1984 | 1977 | 1982 | 1979
university | Harvard | MIT | MIT | Harvard
我已经在我的模板中尝试了foreach和loop组合,但我无法正确显示详细信息部分。
有人知道如何解决这个问题吗?
尝试循环第一个数组的键,然后再循环每个键
{foreach $arr.0 as $row=>$wathever}
<tr><td>{$row}</td>
{foreach $arr as $col}
<td>{$col.$row}</td>
{/foreach}
</tr>
{/foreach}
找到如下解决方案:
{foreach key=key item=item from=$arr}<th>{$item.name}</th>
{/foreach}
{foreach item=item key=key from=$arr.0.details}
<tr><td>{$key}</td>
{section name=arrloop loop=$arr}
<td>{$arr[arrloop].details.$key}</td>
{/section}
</tr>
{/foreach}