我有一个数组。我希望以表格格式显示
日期将在列上,广告客户的名称将在行上。
实施例
DATE | abc | def | xyz
2016-10-26 | 0 | 0 | 0
2016-10-27 | 0 | 0 | 0
数据样本:
Array ( [2016-10-26] => Array ( [abc] => 0 [def] => 0 [xyz] => 0 )
[2016-10-27] => Array ( [abc] => 0 [def] => 0 [xyz] => 0 )
答案 0 :(得分:1)
有许多基于Array的教程,您需要通过它来进行基本的理解。
快速入门
<强> 代码 强>
<?php
$arr["2016-10-26"] = ['abc'=> 0, 'def'=> 0, 'xyz'=> 0];
$arr["2016-10-27"] = ['abc'=> 0, 'def'=> 0, 'xyz'=> 0];
print_r($arr);
?>
<table>
<tr>
<th>Date</th>
<th>abc</th>
<th>def</th>
<th>xyz</th>
</tr>
<?php foreach($arr as $key=>$val){?>
<tr>
<td><?=$key;?></td>
<td><?=$val['abc']?></td>
<td><?=$val['def']?></td>
<td><?=$val['xyz']?></td>
<?php }?>
</tr>
</table>
<强> 输出 强>
数组([2016-10-26] =&gt;数组([abc] =&gt; 0 [def] =&gt; 0 [xyz] =&gt; 0) [2016-10-27] =&gt;数组([abc] =&gt; 0 [def] =&gt; 0 [xyz] =&gt; 0))
Date abc def xyz
2016-10-26 0 0 0
2016-10-27 0 0 0
答案 1 :(得分:0)
您可以尝试使用foreach()函数获取结果。下面的代码可以帮助您。
<?php
$rows = array ('2016-10-26' => array ( 'abc' => 0, 'def' => 0, 'xyz' => 0 ) ,
'2016-10-27' => array ( 'abc' => 0, 'def' => 0, 'xyz' => 0 ));
?>
<table >
<thead>
<th>DATE</th>
<th>abc</th>
<th>def</th>
<th>def</th>
</thead>
<?php foreach($rows as $key=>$val) { ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $val['abc']; ?></td>
<td><?php echo $val['def'] ?></td>
<td><?php echo $val['xyz'] ?></td>
</tr>
<?php } ?>
</table>