用PHP获取朋友的朋友ID?

时间:2018-04-03 01:20:45

标签: php mysql

我的数据库中有一个名为users_friendships的表,基本上如下所示:

id     uid1    uid2
-----  -----   -----
1      1       4
2      1       2
3      2       10
4      3       6
..     ..      ..

通过查询,我已经收到了我朋友的ID。 $myid代表我的身份证明:

    // GET FRIENDS ID
    $friendsID = array();

    $my_friends = $mysql->prepare("
       SELECT CASE WHEN uid1 = ? THEN uid2 
       ELSE uid1 END AS uid 
       FROM users_friendships 
       WHERE uid1 = ? OR uid2 = ?
    ");
    $my_friends->bind_param('iii', $myid, $myid, $myid);
    $my_friends->execute();
    $my_friends_results = $my_friends->get_result();

    while($mf = $my_friends_results->fetch_array()) {

        $mfrid = $mf['uid'];
        $friendsID[] = $mfrid;

    }

我将它们保存在一个数组中,我可以以某种方式使用它来为我朋友的朋友设置查询吗?

2 个答案:

答案 0 :(得分:0)

您可以重复使用现有代码,循环播放您的朋友:

$friendsoffriendsID = array();
foreach ($friendsID as $friendID) {
    $my_friends->bind_param('iii', $friendID, $friendID, $friendID);
    $my_friends->execute();
    $my_friends_results = $my_friends->get_result();
    while($mf = $my_friends_results->fetch_array()) {
        $mfrid = $mf['uid'];
        $friendsoffriendsID[] = $mfrid;
    }
}

这无疑会给你很多重复,所以你可能想要使用array_unique来获取唯一值:

$friendsoffriendsID = array_unique($friendsoffriendsID);

答案 1 :(得分:-2)

我已经更新了我的答案,以便它成为朋友的朋友。我们使用我所做的getFriends()函数来轻松完成此任务。

<?php
function getFriends($userID){
  global $mysql; # get friends of user id
  $friends = $mysql->prepare("SELECT DISTINCT uid1, uid2 FROM users_friendships WHERE uid1 = ? OR uid2 = ?");
  $friends->bind_param("ii", $userID, $userID);
  $friends->execute();
  $friends->store_result();
  if(($numFriends = $friends->num_rows) > 0){
    $friendsArr = array();
    $friends->bind_result($friendID1, $friendID2);
    while($friends->fetch()){
     if($friendID1 == $userID && !in_array($friendID2, $friendsArr)){
      $friendsArr[] = $friendID2;
     } else if($friendID2 == $userID && !in_array($friendID1, $friendsArr)){
      $friendsArr[] = $friendID1;
     }
    }
    $friends->close();
    return $friendsArr; 
  } else { 
    return false;
  }
} // function end

# $myFriends used to store original friendIDs of you, plus all your friend's friends after. Only uniques found... 
if(($myFriends = getFriends($myid)) !== false){ // if array has friend ids, get their friend ids 
  $numFriends = count($myFriends);
  $otherFriends = array(); // array for friends of friends
  for($loopFriends = 0; $loopFriends < $numFriends; $loopFriends++){
    if(($theirFriends = getFriends($myFriends[$loopFriends])) !== false){ # their friend has friends... 
      $numTheir = count($theirFriends);
      for($loopTheir = 0; $loopTheir < $numTheir; $loopTheir++){  // loop through their friends 
        if(!in_array($theirFriends[$loopTheir], $otherFriends) !== false){ // friend id NOT in other friends
          $otherFriends[] = $theirFriends[$loopTheir]; // add their friend to otherfriends 
        }
      } // loop through their friends
    } // their friend has friends
  } // loop my friends

  $numOthers = count($otherFriends);
  for($loopOthers = 0; $loopOthers < $otherFriends; $loopOthers++){
    if(!in_array($otherFriends[$loopOthers], $myFriends) !== false){
      $myFriends[] = $otherFriends[$loopOthers]; # add their friend to the array of all friends
    }
  }

} else {
  // you have no friends 
}

?>