如何在 PHP 中访问 JSON 解码数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15043981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access JSON decoded array in PHP
提问by Chibuzo
I returned an array of JSONdata type from javascriptto PHP, I used json_decode($data, true)to convert it to an associative array, but when I try to use it using the associative index, I get the error "Undefined index"The returned data looks like this
我JSON从javascriptto返回了一个数据类型数组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" }
Please, how do I access such array in PHP? Thanks for any suggestion.
请问,我如何访问这样的数组PHP?感谢您的任何建议。
回答by juco
As you're passing trueas the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
当您将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..
If you do NOT pass trueas the second parameter to json_decodeit would instead return it as an object:
如果您不true作为第二个参数传递给json_decode它,则会将其作为对象返回:
echo $myArray[0]->id;
回答by Norguard
$data = json_decode($json, true);
echo $data[0]["c_name"]; // "John"
$data = json_decode($json);
echo $data[0]->c_name; // "John"
回答by pawan kumar
As you're passing true as the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
当您将 true 作为第二个参数传递给 时json_decode,在上面的示例中,您可以执行类似于以下操作的数据检索:
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
回答by Balázs édes
$data = json_decode(...);
$firstId = $data[0]["id"];
$secondSeatNo = $data[1]["seat_no"];
Just like this :)
像这样 :)
回答by the_butterfly_effect
When you want to loop into a multiple dimensions array, you can use foreach like this:
当你想循环到一个多维数组时,你可以像这样使用 foreach:
foreach($data as $users){
foreach($users as $user){
echo $user['id'].' '.$user['c_name'].' '.$user['seat_no'].'<br/>';
}
}
回答by Gaurav Narula
This may help you!
这可能对你有帮助!
$latlng='{"lat":29.5345741,"lng":75.0342196}';
$latlng=json_decode($latlng,TRUE); // array
echo "Lat=".$latlng['lat'];
echo '<br/>';
echo "Lng=".$latlng['lng'];
echo '<br/>';
$latlng2='{"lat":29.5345741,"lng":75.0342196}';
$latlng2=json_decode($latlng2); // object
echo "Lat=".$latlng2->lat;
echo '<br/>';
echo "Lng=".$latlng2->lng;
echo '<br/>';

