简体   繁体   English

如何基于sql结果分离php数组?

[英]How to separate a php array based on sql results?

I have a PHP array of data (facebook user ids) which I want to compare against the ones stored in my database (which will consist of thousands). 我有一个PHP数据数组(facebook用户ID),我想将其与数据库中存储的数据进行比较(该数据库将包含数千个)。 Of those found in the database, I want them to be separated into a new array. 在数据库中找到的那些对象中,我希望将它们分成一个新的数组。 I have no idea how to go about this. 我不知道该怎么办。

So I begin with this array 所以我从这个数组开始
$ids = 3312312,1232424,1242234,2342636,457456,345345

and end with two 并以两个结尾
$found = 34234234,234234234
$notfound = 23234234,234234,23423423

If anyone could help, that would be great. 如果有人可以帮助,那就太好了。 I've started this a few different ways but not got very far. 我已经以几种不同的方式开始了这个过程,但是距离还很远。 Ideally I'd like this comparison to be done in once but I'm not sure if that's possible. 理想情况下,我希望一次完成一次比较,但是不确定是否可行。

Thanks! 谢谢!

EDIT 编辑

From what you've said, I've come up with the following code quickly. 根据您所说的,我很快提出了以下代码。 It seems to be what you are getting it, but I've not been able to slowly build up to this point so I'm not sure if it's right. 看来这就是您所要获得的,但是到现在为止我还不能慢慢建立,所以我不确定这是否正确。

<?php
$con=mysqli_connect("localhost","root","","sabotage");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//json array is being posted to this file from another page
$jsonarray = '[{"name":"Lizzie OBrien","id":"218101335"},{"name":"Ellis Ward","id":"512376340"}]';
$friendlist = json_decode($jsonarray, true);

$found = [];
$notfound = [];

foreach($friendlist as $friend){

    $friendid = $friend['id'];
    $checkUserID = mysql_query("SELECT facebookid from users WHERE facebookid = '$friendid'");

    if (!$checkUserID) {
        die('Query failed to execute for some reason');
    }

    if (mysql_num_rows($checkUserId) > 0) {
        $found[] = $id;
    }else{
        $notfound[] = $id;
    }

}


mysqli_close($con);

?>

Which gives me: 这给了我:

Query failed to execute for some reason 查询由于某种原因未能执行

Does it make a difference that my facebookid column is an Integer? 我的facebookid列是Integer会有所不同吗?

Thanks 谢谢

How I would do it: 我将如何做:

$idsfromdb; //grab all ids from the db, and put in array
$idstobetested; //array of all ids you want to compare
$found = [];
$notfound[];

foreach($idstobetested as $id){
  if(in_array($id, $idsfromdb)){
    $found[] = $id;
  }else{
    $notfound[] = $id;
  }
}

However: 然而:

After seeing your comment, if your db has a large number of records, instead of selecting them all and putting it into an array. 在看到您的注释后,如果您的数据库具有大量记录,则不要全部选中它们并将其放入数组中。 Instead, iterate through your array of ids that you want to test and run a select query on the db, if that does not return false, the value exists in the db and you can then push the id to the found array. 取而代之的是,遍历要测试的id数组并在db上运行一个选择查询,如果没有返回false,则该值存在于db中,然后可以将id推送到找到的数组。

This may be of use: How to check if value already exists in MySQL database 这可能有用: 如何检查MySQL数据库中是否已存在值

You might be looking for this functions. 您可能正在寻找此功能。

$jsonarray = '[{"name":"Lizzie OBrien","id":"218101335"},{"name":"Ellis Ward","id":"512376340"}]';
$friendlist = json_decode($jsonarray, true);

$friendIds = array_map( create_function('$data', 'return $data["id"];'), $friendlist);

// Will return all the matched records
$sql1 = "SELECT yourcolumnname FROM yourtablename WHERE facebookid IN (".implode(',', $friendIds).")";

// Will return all the unmatched records
$sql2 = "SELECT yourcolumnname FROM yourtablename WHERE facebookid NOT IN (".implode(',', $friendIds).")";

This is the code that did it for me. 这是为我完成的代码。 Couldn't have done it without your help. 没有您的帮助就做不到。

<?php
$con=mysql_connect("localhost","root","");
// Check connection
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

//json array is being posted to this file from another page
$jsonarray = '[{"name":"Lizzie OBrien","id":"218101335"},{"name":"Ellis Ward","id":"512376340"}]';
$friendlist = json_decode($jsonarray, true);


$found = [];
$notfound = [];

foreach($friendlist as $friend){

    $friendid = $friend['id'];
    mysql_select_db("sabotage", $con);
    $result = mysql_query("SELECT facebookid FROM users WHERE facebookid ='$friendid'", $con);

    if (mysql_num_rows($result) > 0) {
        $found[] = $friendid;
    }else{
        $notfound[] = $friendid;
    }

}

mysql_close($con);

?>

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

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