0

我有以下示例 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,如果是,如何?

4

1 回答 1

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";
}
?>
于 2013-05-26T17:32:14.123 回答