What I have here is a PHP page working with the Facebook API.
What I’m trying to do is (after permissions are set by the user), to get the user’s friends’ user IDs via: $facebook->api('/me/friends'). The problem is, I would only like to get random 10 friends. I could easily limit results to 10 by using /me/friends?limit=10, but then again that wouldn’t be random.
So here is what I have right now:
$friendsLists = $facebook->api('/me/friends');
function getFriends($friendsLists){
foreach ($friendsLists as $friends) {
foreach ($friends as $friend) {
// do something with the friend, but you only have id and name
$id = $friend['id'];
$name = $friend['name'];
shuffle($id);
return "@[".$id.":0],";
}
}
}
$friendsies = getFriends($friendsLists);
$message = 'I found this Cover at <3 '.$Link.'
'.$friendsies.' check it out! :)';
I have tried shuffle(), and the first option from here: https://stackoverflow.com/a/1656983/1399030, but I think I could be doing something wrong because they don’t return anything. I’m pretty sure I’m close, but what I’ve tried so far is not working. Can it be done?
you’ll want to use the shuffle before you foreach, so that you actually shuffle the array.
after that, you’ll want to limit to 10 friends. I’d suggest adding an $i var to count to ten, and adding to a new array.
Something like this:
keep in mind though that it’ll return an array and not a string that you can use in an echo. If you want, you can put this in an echo for debugging purposes: