我的网站回复为
[{"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){
...
}
当我运行此代码时,出现错误Invalid argument supplied for foreach()
这是什么问题?
如何在html表格中显示为表格?
答案 0 :(得分:1)
这段代码完全正常 - 只需在本地机器上测试它
<?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_decode将失败并且foreach()循环将引发错误。
答案 1 :(得分:-1)
你在问题中提到的代码是完美的,我得到表中所需的输出。但请确保检查json_encode()
是否正确,以便foreach
错误隐藏起来。
您也可以使用此方法,因为我在此处建议在表格中打印。
您必须将TRUE
应用于json_decode
语句,因为std_object数组与此相关,因此会导致打印数组值时产生混淆。
<强> json_decode()强>
Syntax: json_decode($jsonstring,TRUE);
json_decode - 解码JSON字符串
以适当的PHP类型返回json中编码的值。值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
}
?>
在json_decode()语句中输出没有TRUE的print_r():
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>
我希望我的解决方案可以满足您的需求。
快乐编码:)