[英]How to convert JSON array to HTML table
I have the following function in a PHP script, which returns and API call: 我在PHP脚本中具有以下函数,该函数返回和API调用:
if (in_array((int)$result['code'], array(200, 201))) {
$presult=(json_decode($result['body'],true));
print_r($presult);
} else {
print("error parsing result");
var_dump($result);
}
$presult displays raw data in the following format: $ presult以以下格式显示原始数据:
Array
(
[contacts] => Array
(
[users] => Array
(
[0] => Array
(
[first_name] => Peter
[last_name] => Parker
I want to print this array in an HTML table with columns serial no., First name, Last 我想在具有序列号,名字,姓氏列的HTML表中打印此数组
I guess the array index is the serial number, so you can do this: 我猜数组索引是序列号,因此您可以执行以下操作:
echo '<table>
<tr>
<th>Serial Number</th>
<th>First name</th>
<th>Last name</th>
</tr>';
foreach($presult['contacts']['users'] as $number => $user){
echo '<tr>
<td>' . $number . '</td>
<td>' . $user['first_name'] . '</td>
<td>' . $user['last_name'] . '</td>
</tr>';
}
echo '</table>';
And don't forget to escape the HTML output if required. 并且,如果需要,不要忘记转义HTML输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.