Php 将多维关联数组显示为表
我有以下数组,我想以HTML表格格式显示它。例如:Php 将多维关联数组显示为表,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有以下数组,我想以HTML表格格式显示它。例如: <table> <tr> <th>Case Number</td> <th>Created On</td> <th>
<table>
<tr>
<th>Case Number</td>
<th>Created On</td>
<th>Fault Type</td>
<th>Status</td>
</tr>
<tr>
<td id="fcase1">Case Number</td>
<td id="creat1">Created On</td>
<td id="ftype1">Fault Type</td>
<td id="stats1">Status</td>
</tr>
)
我试过这个:
echo '<table><tr>';
function process_array($array){
foreach($array as $key => $value){
if(is_array($value)){
process_array($value);
}else{
echo '<td>'. $value . '</td>';
}
}
}
process_array($result);
echo '</tr></table>';
echo';
函数进程\u数组($array){
foreach($key=>$value的数组){
if(是_数组($value)){
进程_数组($value);
}否则{
回显“.$value.”;
}
}
}
处理数组($result);
回声';
但是我没有得到正确的结果。您可以通过索引以简单的方式撬动它:
<table>
<tr>
<th>Case Number</th>
<th>Created On</th>
<th>Fault Type</th>
<th>Status</th>
</tr>
<?php
foreach ($array['GetRecentCasesResult']['CaseHistory'] as $data) {
?>
<tr>
<td id="fcase1"><?php echo $data['caseNumber']; ?>Case Number</td>
<td id="creat1"><?php echo $data['dateCreated']; ?>Created On</td>
<td id="ftype1"><?php echo $data['faultType']; ?>Fault Type</td>
<td id="stats1"><?php echo $data['status']; ?>Status</td>
</tr>
<?php
}
?>
</table>
案件编号
创建于
故障类型
地位
案件编号
创建于
故障类型
地位
这应该行得通
<table>
<tr>
<th>Case Number</td>
<th>Created On</td>
<th>Fault Type</td>
<th>Status</td>
</tr>
<?php foreach ($array['GetRecentCasesResult']['CaseHistory'] as $key => $value) { ?>
<tr>
<td id="fcase<?php echo $key; ?>"><?php echo $value['caseNumber']; ?></td>
<td id="creat<?php echo $key; ?>"><?php echo $value['dateCreated']; ?></td>
<td id="ftype<?php echo $key; ?>"><?php echo $value['faultType']; ?></td>
<td id="stats<?php echo $key; ?>"><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</table>
案件编号
创建于
故障类型
地位
为什么不使用简单的foreach外观而不是递归函数。即使这样也是正确的。只有id
也需要是动态的,因为单个html页面上不应该有相同的id
<table>
<tr>
<th>Case Number</td>
<th>Created On</td>
<th>Fault Type</td>
<th>Status</td>
</tr>
<?php foreach ($array['GetRecentCasesResult']['CaseHistory'] as $key => $value) { ?>
<tr>
<td id="fcase<?php echo $key; ?>"><?php echo $value['caseNumber']; ?></td>
<td id="creat<?php echo $key; ?>"><?php echo $value['dateCreated']; ?></td>
<td id="ftype<?php echo $key; ?>"><?php echo $value['faultType']; ?></td>
<td id="stats<?php echo $key; ?>"><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</table>