I have converted a GraphObject into an array.
How do I get just the names instead of the whole array? Like: You are friend with..
Bruce Willis
John Travolta
How my code looks like:
$friends = (new FacebookRequest( $session, 'GET', '/me/friends' ))
->execute()
->getGraphObject()
->asArray();
echo '<pre>' . print_r( $friends,1 ) . '</pre>';
And the results looks like this for the moment:
foreach ($friends['data'] as $friend) {
echo $friend->name . "<br />";
}
You want to loop through the data array, and store them all in the variable $friend
, $friend
is an object with id
and name
attribute, so you can call them like this: $friend->name
and $friend->id
Iterate over the data
array and fetch the name property of each object.
Here's an example of getting all names into another array:
$namesArray = [];
foreach($friends['data'] as $f) {
$namesArray[] = $f->name;
}
// print
echo '<pre>', print_r( $namesArray,1 ), '</pre>';
If you want the associative array (id=>name), you can do this:
foreach($friends['data'] as $f) {
$namesArray[$f->id] = $f->name;
}