PHP从mysqli_fetch_array中删除forEach循环中的值

时间:2016-06-07 19:37:44

标签: php arrays mysqli foreach

我在SQL数据库上运行以下查询。我接下来要做的是从$result_users中移除user_ID receiverID数组$result_sender中的所有用户。为此,我创建了以下代码,但我不确定我的方法是否正确。我在网上发现应该做的是unset($array[$key]),但是我发现的所有例子都是普通数组,而不是从SQL数据库中获取的,因此我不确定如何在这种情况下继续

 $retrieve_potential_matches_sender = mysqli_query($conn,"SELECT senderID, receiverID FROM match_instance WHERE senderID = '$sender'");
 $retrieve_all_users = mysqli_query($conn,"SELECT user_id, sex, latitude, longitude FROM users_with_fb");
 $result_sender = mysqli_fetch_array($retrieve_potential_matches_sender);
 $result_users = mysqli_fetch_array($retrieve_all_users);

 if count($result_sender) > 0 { 

 foreach ($result_sender as $sendingID) {

   foreach ($result_users as $userValue) { 

     if ($userValue["user_id"] == $sendingID["receiverID"] {

       unset($userValue);
    }
   }
 }

2 个答案:

答案 0 :(得分:1)

您可以执行以下子查询:

SELECT user_id, sex, latitude, longitude FROM users_with_fb WHERE user_id NOT IN (SELECT receiverID FROM match_instance);

答案 1 :(得分:0)

您正在取消循环使用的变量,而不是实际数组中的值。这应该是你要找的东西:

foreach ($result_sender as $sendingID) {

  foreach ($result_users as $key => $userValue) { 

     if ($userValue["user_id"] == $sendingID["receiverID"] {

       unset($result_users[$key]);
     }
  }
}