我返回数组JSON
数据类型从javascript
到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" }
Run Code Online (Sandbox Code Playgroud)
请问,如何访问这样的阵列PHP
?谢谢你的任何建议.
juc*_*uco 57
当您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..
Run Code Online (Sandbox Code Playgroud)
如果你没有true
作为第二个参数传递给json_decode
它,而是将它作为一个对象返回:
echo $myArray[0]->id;
Run Code Online (Sandbox Code Playgroud)
$data = json_decode($json, true);
echo $data[0]["c_name"]; // "John"
$data = json_decode($json);
echo $data[0]->c_name; // "John"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
123831 次 |
最近记录: |