How can I parse JSON output from Facebook php graph api? -
i have simple code example sdk facebook:
if ($user) { try { $user_profile = $facebook->api('/me/friends'); } catch (facebookapiexception $e) { error_log($e); $user = null; } } if print $user_profile result list of friends in format:
[206] => array ( [name] => name [id] => 00000000 ) is possible access fields of these arrays? example want print friends' names
to print friends' names, loop through main array/object:
foreach($user_profile $k => $friend) { echo $friend['name'].' - '.$user_profile[$key]['id'].'<br/>'; } to access specific friend:
echo $user_profile[11]['name']; this echoes name of 11th friend in array. combine 2 index of given friend using function (or method):
public function getfriendindex($name) { foreach($this->user_profile $index => $friend) { if ($friend['name'] === $name) {//returns index of $name's friend data return $index; } } return null;//friend doesn't exist... }
Comments
Post a Comment