我有一個JSON文件(test.json),這是JSON的foreach只得到第一個結果出來一個數組
{
"fruit": [{
"id": 364823,
"name": "Lemon",
"amount": [33],
"profile": "http://google.com",
"buyid": 5
}, {
"id": 367851,
"name": "Orange",
"amount": [69, 95, 166],
"profile": "http://google.com",
"buyid": 3
},{
"id": 35647,
"name": "Apple",
"amount": [77, 43],
"profile": "http://google.com",
"buyid": 31
} ]
}
那麼我有我的PHP腳本來呼應
$url="test.json";
$json = file_get_contents($url);
$json = json_decode($json, true);
$names = array();
foreach($json['fruit'][0]['amount'] as $val)
{
echo $val . " <br> ";
}
它返回
33
我怎樣才能得到它返回?
33
69 95 166
77 43
我可以得到它與其他工作一樣ID,輪廓和buyid但不是這個數組
爲什麼 「JavaScript的」 標籤?關於你的要求,你不需要嵌套循環嗎? – nnnnnn
'['fruit'] [0] ['amount']'數組只有一個元素''amount':' –
或者在第一級添加另一個指向'amount'或implode的'foreach'維度 – Ghost