简体   繁体   中英

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). 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?

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.

This may be of use: How to check if value already exists in MySQL database

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);

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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