从JSON问题中提取

时间:2013-05-26 17:26:44

标签: php json

我有以下示例JSON数据:

Array ( [data] => Array ( [0] => Array ( [name] => Jonathan James[id] => 45879) [1] => Array ( [name] => Jackson C. Gomez [id] => 989) [2] => Array ( [name] => Liz Mack [id] => 787)... etc etc

我正在尝试遍历它并提取名称和ID但没有返回任何内容,这是我正在尝试的代码:

$friendsList = json_decode(file_get_contents('https://graph.facebook.com/'.$fb_user.'/friends?access_token='.$access_token),true);

foreach ($friendsList as $element){

    echo $element[name].' - '.$element[id];

}

我是否需要深入研究JSON,如果是这样的话?

1 个答案:

答案 0 :(得分:1)

请试试这个:

<?php
$friendsList = array(
    'data' => array(
        array('name' => 'Jonathan James', 'id' => 45879),
        array('name' => 'Jackson C. Gomez', 'id' => 989),
        array('name' => 'Liz Mack', 'id' => 787)
    )
);

foreach ($friendsList['data'] as $element){
    echo $element['name'].' - '.$element['id']."<br>\n";
}
?>