我有一个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