Json将print_r拆分为foreach

时间:2012-04-24 14:40:47

标签: php foreach json

我有以下代码

$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()

我很难过,这可能很简单!

2 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:0)

在此示例中,&#39;数据&#39;密钥不存在

$data = $List2;