2012-04-24 64 views
0

我有以下代碼的Json分裂的print_r到FOREACH

$List2 = json_decode(file_get_contents("https://___URL____&format=json"),true); 

以下作品完美

echo '<pre>';print_r($List2);echo '</pre>'; 

和例如產生

Array 
(
[0] => Array 
    (
     [uid] => 123456 
     [name] => John Williams 
     [pic_square] => http://nc4/565228_799523_q.jpg 
     [birthday_date] => 07/31/1987 
    ) 

[1] => Array 
    (
     [uid] => 123789 
     [name] => Jane Thompson 
     [pic_square] => http://profile.ak.fbcdn.net/785505233_1702140670_q.jpg 
     [birthday_date] => 07/31/1983 
    ) 

[2] => Array 
    (
     [uid] => 456789 
     [name] => John Gaffney 
     [pic_square] => http://profet/hprofile-ak-snc4/3717297628_q.jpg 
     [birthday_date] => 07/31/1965 
    ) 

[3] => Array 
    (
     [uid] => 987654 
     [name] => Johnny Illand 
     [pic_square] => http://c4/41766_14329_q.jpg 
     [birthday_date] => 07/31/1958 
    ) 

我想運行一個foreach有所打印結果整潔明顯,所以我想以下幾點:

$data = $List2['data'] ; 

foreach ($data as $nr => $friends){ 

echo $friends[name].' - '; 

} 

,但我得到

Warning: Invalid argument supplied for foreach() 

我難倒這可能很簡單!

+1

您應該簡單地使用'$ List2'代替'$ data'(其中還將是'null')。您正在打印一件東西,然後迭代另一件東西。結果令人驚訝嗎? – Jon 2012-04-24 14:42:36

回答

1

與嘗試:

foreach ($List2 as $element) 
    echo $element[name].' - '; 
0

在這個例子中, '數據' 項不存在

$data = $List2;