我从javascript返回了一个JSON数据类型的数组到PHP,我使用json_decode($data, true)将其转换为关联数组,但是当我尝试使用关联index使用它时,我得到错误"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中访问这样的数组?谢谢你的建议。
发布于 2013-02-24 02:28:52
当您将true作为第二个参数传递给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..echo $myArray[0]->id;发布于 2013-02-24 02:28:51
$data = json_decode($json, true);
echo $data[0]["c_name"]; // "John"
$data = json_decode($json);
echo $data[0]->c_name; // "John"发布于 2013-02-24 02:28:35
$data = json_decode(...);
$firstId = $data[0]["id"];
$secondSeatNo = $data[1]["seat_no"];就像这样:)
https://stackoverflow.com/questions/15043981
复制相似问题