0

I successfully loaded the friends array containing more arrays into a variable. But how do I iterate through it to take only the id's out?

I used:

$friends = $facebook->api('/me/friends/');

It returns a multidimensional array like so:

array(1) { ["name"] => "Username" ["id"] => "User ID #"}

array(2) { ["name"] => "Username" ["id"] => "User ID #"} 

...and so on...

Any help would be appreciated! Thanks.

2 Answers 2

1

Results of Graph API almost always contain data property which is contain all the response data, to get only the friends ids you can look over it in such way:

// If all you need is users ids, specify it in fields url argument
$friends = $facebook->api('/me/friends/?fields=id');
$data = $friends['response'];
$friendsIds = array();
for ($data as $user){
  $friendsIds[] = $user['id'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

$yourIdArr = array();
foreach($yourFriendArray as $key => $val) {
 if("id" == $key) {
    array_push($yourIdArr, $val);
 }
}

3 Comments

Hmm just tried it and I get an empty Array when I use print_r...any other suggestions? Thanks for the help!
Are your doing your foreach with $friends (result from $facebook->api('/me/friends/') ) ? Isn't there a ['data'] index on it where all the sub-arrays are stored ?
Your sample is wrong! $val is really an array which contain id key, $key in this case is a numeric index...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.