问题描述:

I want to know how much friends a user has, to select a random friend.

I'm working with the Facebook API and I'm doing this call

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

Wich returns the array $friendlist

Array (

[data] => Array (

[0] => Array (

[name] => User Name

[id] => XXXXXXXX

)

[1] => Array (

[name] => User name

[id] => XXXXXXXXXX

)

[2] => Array (

[name] => User Name

[id] => XXXXXXXX

)

)

[paging] => Array (

[next] => https://graph.facebook.com/1453123479/friendslimit=5000&offset=5000&__after_id=1000059235606

)

)

I'm trying this - as stated here - but it doesn't echo anything.

echo count($friendlist['data']);

Any help is much appreciated!

Stijn

网友答案:

If you would like to select a random friend, you can use the built in PHP function rand() to get a randomly selected index which you can use in your variable $friendlist.

function getRandomFriend() {
    $friendlist = $this->facebook->api( '/me/friends' );
    // Count the number of friends
    $numFriends = count( $friendlist[ "data" ] );
    // Random Number
    $randNum = rand( 0, $numFriends );
    return $friendlist[ "data" ][ $randNum ];
}
网友答案:

You're outputting $data['friendlist'] to get that array so I'd expect to use the following:

echo count( $data['friendlist']['data'] );
网友答案:

Found it!

The number was added to the id - so I couldn't see it - as you can see here:

echo "my id is " . $user_id;

echo count($friendlist['data']);

I'm embarrassed, such a stupid mistake :(.

Thanks for the input tho!

Stijn

相关阅读:
Top