2016-07-25 67 views
-2

我有一个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但不是这个数组

+1

为什么 “JavaScript的” 标签?关于你的要求,你不需要嵌套循环吗? – nnnnnn

+0

'['fruit'] [0] ['amount']'数组只有一个元素''amount':' –

+0

或者在第一级添加另一个指向'amount'或implode的'foreach'维度 – Ghost

回答

3
foreach($json['fruit'] as $fruit){ 
    echo implode(' ',$fruit['amount']); 
} 

Live demo

+0

工作过,谢谢! – C0ft

+0

不客气:-)如果它解决了你的问题,不要忘记选择这个答案。 – BeetleJuice

相关问题