我试图通过一堆这样的JSON代码(通过url)来预测PHP循环;
{
"abc": [{
"a": "1",
"b": [{
"ba": 1,
"bb": 2
}],
"c": 3,
"d": [{
"da": 4,
"db": 5
},
{
"dc": 6,
"dd": 7
}],
"e": 8,
"f": 9,
}]
我能够获得“a”,“c”,“e”和“f”的关键值。
我使用此代码来执行此操作;
$url = 'http://url.com/json';
$jsondata = file_get_contents($url);
$jso = json_decode($jsondata, TRUE);
$data = $json['abc'];
foreach ($data as $feature)
{
echo $feature['a'];
}
当我尝试echo $feature['b']
时,会显示“数组”。 $feature['ba']
显示Undefined index: ba in <b>x:/test.php</b> on line x
答案 0 :(得分:1)
如果你循环$json["abc"][0]
,那么键将是a,b,c,d,e和f。
$url = 'http://url.com/json';
$jsondata = file_get_contents($url);
$json = json_decode($jsondata, TRUE);
$data = $json['abc'][0];
foreach ($data as $key => $value) {
var_dump($key, $value);
}
"ba"
将是:$json["abc"][0]["b"][0]["ba"]
"dc"
将是:$json["abc"][0]["d"][1]["dc"]