简体   繁体   English

随机化/随机排列数组,然后进行foreach,然后限制为10个结果(Facebook API + PHP)

[英]Randomize/Shuffle array, then foreach, then limit to 10 results (Facebook API + PHP)

What I have here is a PHP page working with the Facebook API. 我在这里有一个使用Facebook API的PHP页面。

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') . 我想做的是(在用户设置权限之后),通过以下方式获取用户朋友的用户ID: $facebook->api('/me/friends') The problem is, I would only like to get random 10 friends . 问题是,我只想随机获得10个朋友 I could easily limit results to 10 by using /me/friends?limit=10 , but then again that wouldn't be random. 我可以使用/me/friends?limit=10轻松地将结果/me/friends?limit=10 ,但是那又不是随机的。

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. 我尝试过shuffle(),以及这里的第一个选项: https : //stackoverflow.com/a/1656983/1399030 ,但是我认为我做错了什么,因为它们不返回任何东西。 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. 之后,您最多只能允许10个朋友。 I'd suggest adding an $i var to count to ten, and adding to a new array. 我建议添加一个$ i var以计数到10,然后添加到一个新数组中。

Something like this: 像这样:

function getFriends($friendsLists){
   $formatted_friends = array();
   $i = 0;
   foreach ($friendsLists as $friends) {
      // I'm guessing we'll need to shuffle here, but might also be before the previous foreach
      shuffle($friends);
      foreach ($friends as $friend) {
         // do something with the friend, but you only have id and name
         // add friend as one of the ten
         $formatted_friends[$i] = $friend;
         // keep track of the count
         $i++;
         // once we hit 10 friends, return the result in an array
         if ($i == 10){ return $formatted_friends; }
      }
   }
 }

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: 如果需要,可以将其放入回显中以进行调试:

echo 'friends: '.print_r($friendsies, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM