我在這裏是一個PHP頁面與Facebook API一起工作。Randomize/Shuffle數組,然後foreach,然後限制到10個結果(Facebook API + PHP)
我想要做的是(在權限由用戶設置後),通過$facebook->api('/me/friends')
獲取用戶的朋友的用戶ID。問題是,我只想得到隨機10位朋友。我可以很容易地通過使用/me/friends?limit=10
將結果限制爲10,但是再次這不會是隨機的。
所以這是我現在所擁有的:
$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! :)';
我試圖洗牌(),並從這裏第一個選項:https://stackoverflow.com/a/1656983/1399030,但我認爲因爲他們沒有我做錯了什麼回報什麼。我很確定我很近,但我迄今爲止嘗試過的方法並不奏效。可以做到嗎?
的可能重複[Facebook的圖形API:獲得一個隨機N的朋友(http://stackoverflow.com/questions/8570459/facebook -graph-api-getting-a-random-n-friends) –
@EvanMulawski希望沒有使用FQL查詢。由於我已經有了friendslist數組,我只需要知道一種方法來洗牌。 – Mafia