加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中访问JSON解码的数组

发布时间:2020-12-13 16:29:13 所属栏目:PHP教程 来源:网络整理
导读:我从 javascript到 PHP返回了JSON数据类型的数组,我使用json_decode($data,true)将其转换为关联数组,但是当我尝试使用关联索引使用它时,我得到错误“Undefined index”返回的数据看起来像这样 array(14) { [0]= array(4) { ["id"]= string(3) "597" ["c_name
我从 javascript到 PHP返回了JSON数据类型的数组,我使用json_decode($data,true)将其转换为关联数组,但是当我尝试使用关联索引使用它时,我得到错误“Undefined index”返回的数据看起来像这样
array(14) { [0]=> array(4) { ["id"]=> string(3) "597" ["c_name"]=> string(4) "John" ["next_of_kin"]=> string(10) "5874594793" ["seat_no"]=> string(1) "4" } 
[1]=> array(4) { ["id"]=> string(3) "599" ["c_name"]=> string(6) "George" ["next_of_kin"]=> string(7) "6544539" ["seat_no"]=> string(1) "2" } 
[2]=> array(4) { ["id"]=> string(3) "601" ["c_name"]=> string(5) "Emeka" ["next_of_kin"]=> string(10) "5457394839" ["seat_no"]=> string(1) "9" } 
[3]=> array(4) { ["id"]=> string(3) "603" ["c_name"]=> string(8) "Chijioke" ["next_of_kin"]=> string(9) "653487309" ["seat_no"]=> string(1) "1" }

请问如何在PHP中访问这样的数组?感谢任何建议.

当你作为第二个参数传递给json_decode时,在上面的例子中,您可以检索类似于以下内容的数据:
$myArray = json_decode($data,true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['c_name']; // Fetches the first c_name
// ...
echo $myArray[2]['id']; // Fetches the third ID
// etc..

如果没有将第二个参数传递给json_decode,那么它将返回它作为一个对象:

echo $myArray[0]->id;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读