如何在PHP中从多维数组创建html表?
我正在向服务器发送一个HTTP请求,并得到一个json格式的响应。我使用以下方法将其转换为数组:如何在PHP中从多维数组创建html表?,php,arrays,for-loop,html-table,Php,Arrays,For Loop,Html Table,我正在向服务器发送一个HTTP请求,并得到一个json格式的响应。我使用以下方法将其转换为数组: $response = file_get_contents($final_url); $responseArray = json_decode($response, true); 这是服务器响应数组$responseArray 现在,如果[status]=>OK,我想以html表格格式打印'result'数组。我怎么能做到 只要添加一个if条件,如果它满足,然后迭代所述数组,指向适当的索引: if
$response = file_get_contents($final_url);
$responseArray = json_decode($response, true);
这是服务器响应数组$responseArray
现在,如果[status]=>OK,我想以html表格格式打印'result'数组。我怎么能做到 只要添加一个if条件,如果它满足,然后迭代所述数组,指向适当的索引:
if($responseArray['status'] === 'OK') {
foreach($responseArray['result'] as $result) {
echo $result['name']; // and other indices
}
}
将其打印到表标记中只是创建包含标记的普通表的过程
超级粗略的例子:
<?php if($responseArray['status'] === 'OK'): ?>
<table>
<thead>
<tr>
<?php foreach($headers as $h): ?>
<th><?php echo $h; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($responseArray['result'] as $row): ?>
<tr>
<?php foreach($row as $value); ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
@比卡什玛哈塔很高兴这对你好鬼有帮助。帮助我1个更多的数组[状态]=>OK[结果]=>Array[pnr]=>4106121143[cls]=>3A[eticket]=>1[trainno]=>2015年4月9日[trainno]=>12740[name]=>VSKP GARIB RATH[from]=>SC[to]=>VSKP[brdg]=>SC[乘客]=>Array[0]=>Array[bookingstatus]=>G14 52GN[currentstatus]=>Can/Mod[coach]=>1]=>Array[bookingstatus]=>G14 54GN[currentstatus]=>Can/Mod[coach]=>chart PREPARED[error]=>@BikashMahata当一个数组中引入另一个级别时,只需在其中添加另一个foreach,它将与$val{echo$val['bookingstatus']的foreach$row['Passenger']工作相同;我已经添加:if$responseArray['status']=='OK'{foreach$responseArray['result']作为$result{echo;echo.$result['旅程]];echo;}
<?php if($responseArray['status'] === 'OK'): ?>
<table>
<thead>
<tr>
<?php foreach($headers as $h): ?>
<th><?php echo $h; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($responseArray['result'] as $row): ?>
<tr>
<?php foreach($row as $value); ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>