StudioTime
StudioTime

Reputation: 23989

Extracting from JSON issue

I have the following example JSON data:

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

I'm trying to loop through it and pull the name and the ID but returning nothing, here's the code I'm trying:

$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];

}

Do I have to drill deeper into the JSON and if so how?

Upvotes: 0

Views: 35

Answers (1)

Dave Chen
Dave Chen

Reputation: 10975

Please try this:

<?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";
}
?>

Upvotes: 1

Related Questions