PHP-在html表中显示JSON数组
我从一个网站上得到了如下回复:PHP-在html表中显示JSON数组,php,arrays,json,Php,Arrays,Json,我从一个网站上得到了如下回复: [{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}...] 我的代码是 $json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"4
[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}...]
我的代码是
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
foreach($json_decoded as $result){
...
}
当我运行这段代码时,我得到一个错误为foreach()提供的无效参数
这里的问题是什么
如何在html表格中显示为表格?这段代码工作得非常好-只需在本地机器上进行测试即可
<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
foreach($json_decoded as $result){
print_r($result);
}
?>
要将内容输出为表,请使用以下命令:
<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
echo '<table>';
foreach($json_decoded as $result){
echo '<tr>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->phone.'</td>';
echo '<td>'.$result->email.'</td>';
echo '</tr>';
}
echo '</table>';
?>
确保JSON字符串没有任何语法错误。。。如果是,json_解码将失败,foreach()循环将抛出一个错误。您在问题中提到的代码工作正常,我将按照表中的要求获得输出。但是确保检查json\u encode()
是否正确,以便foreach
错误隐藏起来
您也可以使用我在这里建议的方法在表中打印
您必须将TRUE
应用于json\u decode
语句,因为std\u对象数组与之相关,因此在打印数组值时会导致混淆
json_decode()
json_decode-解码json字符串
返回用json编码的适当PHP类型的值。值true、false和null分别作为true、false和null返回。如果无法解码json或编码数据深度超过递归限制,则返回NULL
PHP代码:
<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json,TRUE);
foreach($json_decoded as $result){
echo $result['name']; // this wil output the values as xxxyyyzzz
}
?>
Array ( [0] => stdClass Object ( [name] => xxx [phone] => 123 [email] => a@a.com ) [1] => stdClass Object ( [name] => yyy [phone] => 456 [email] => b@a.com ) [2] => stdClass Object ( [name] => zzz [phone] => 678 [email] => c@a.com ) )
在json_decode()语句中使用TRUE输出print_r()
Array ( [0] => Array ( [name] => xxx [phone] => 123 [email] => a@a.com ) [1] => Array ( [name] => yyy [phone] => 456 [email] => b@a.com ) [2] => Array ( [name] => zzz [phone] => 678 [email] => c@a.com ) )
因此,上述代码的输出如下所示
xxxyyyzzz
您可以毫不费力地循环查看foreach
数据,并使用它进行打印
打印表格中的数据,您可以按照下面提到的代码进行操作
<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json,TRUE);
?>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
foreach($json_decoded as $result){
?>
<tr>
<td><?php echo $result['name']; ?></td>
<td><?php echo $result['phone']; ?></td>
<td><?php echo $result['email']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
名称
电话
电子邮件
我希望我的解决方案能满足您的需求
快乐编码:)你检查过字符串是否为有效的json吗?试试foreach($json as$result){…}我刚刚复制并粘贴了它,它对我有效…你应该发布真实数据,因为这不会有问题。您的json无效或结构与您显示的不一样。同意@jeroen-您上面的代码工作得非常好,我觉得您没有向我们显示某些内容。这根本没有解决OP存在的问题:为foreach()提供的参数无效。
。根本不可能@杰罗恩。此代码将以最佳和高效的方式工作。我有证据,我已经测试过了。不要像这样责怪你,你会得到像这样的错误等等。它会起作用,否则你会告诉我你在我的代码中哪里得到错误。我已经展示了所有的结果和我得到的证据。你没有任何意义,OP有一个错误并且发布了一个问题。这个答案并没有解决这个问题。我已经测试过了,我所做的代码没有出现任何错误。这就是我将其作为答案发布的原因。谢谢。实际上,我的json数据的值中有一些错误。我清理了数据,代码运行良好。
<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json,TRUE);
?>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
foreach($json_decoded as $result){
?>
<tr>
<td><?php echo $result['name']; ?></td>
<td><?php echo $result['phone']; ?></td>
<td><?php echo $result['email']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>