简体   繁体   中英

How to compare different arrays?

I need to show the email address, first and last names of users who are not registered in my new database.

I selected three columns of my old database and three columns of my new database. I created two arrays and each receives the result of the query.

But when the comparing is displaying all users, the comparison is not made.

Check my code:

while($array = $resultado->fetch_array(MYSQLI_NUM)){
$portal_old[] = $array;
}
while($array2 = $resultado2->fetch_array(MYSQLI_NUM)){
$portal_new[] = $array2;
}
foreach ($portal_old as $portal) {
   if(!in_array($portal, $portal_new)){
    $result[] = $portal;
  }


}

Assuming email address should be able to uniquely identify a registered user, you can use email address as a key as you build your array of results from each database.

while($array = $resultado->fetch_array(MYSQLI_ASSOC)){
    $portal_old[$array['email']] = $array;
}
while($array2 = $resultado2->fetch_array(MYSQLI_ASSOC)){
    $portal_new[$array2['email']] = $array2;
}

In order for this to work, you will need to either use MYSQLI_ASSOC instead of MYSQLI_NUM when fetching, and specify the name of the email column when building your array, or if you are using MYSQLI_NUM , specify the numeric index of the email column.

Then you can use array_diff_key to easily find the result you are looking for.

$result = array_diff_key($portal_old, $portal_new);

This works, John Smith here gets in $result while Joe Black doesn't:

<?php
$name = array(0 => 'john', 1 => 'smith');
$portal_old[] = $name;
$name = array(0 => 'joe', 1 => 'black');
$portal_old[] = $name;

//print_r($portal_old);

//shows: Array ( [0] => Array ( [0] => john [1] => smith ) [1] => Array ( [0] => joe [1] => black ) ) 

$name = array(0 => 'jason', 1 => 'mill');
$portal_new[] = $name;
$name = array(0 => 'joe', 1 => 'black');
$portal_new[] = $name;

//print_r($portal_new);

//shows: Array ( [0] => Array ( [0] => jason [1] => mill ) [1] => Array ( [0] => joe [1] => black ) ) 

foreach($portal_old as $key=>$value) {
    if(!in_array($value[0], $portal_new[0]) && !in_array($value[1], $portal_new[1])) {
        $result[] = $value;
    }
}

print_r($result);

//shows:  Array ( [0] => Array ( [0] => john [1] => smith ) ) 

?>

It's because the results of $portal_old and $portal_new are multidimensional arrays, you need to compare by looking inside the arrays of the arrays.

This will work for you :

$i = $j = 0;
while($array = $resultado->fetch_array(MYSQLI_NUM)){
$portal_old[$i][$array['0'].' | '.$array['1'].' | '.$array['2']] = $array;
}
while($array2 = $resultado2->fetch_array(MYSQLI_NUM)){
$portal_new[$j][$array2['0'].' | '.$array2['1'].' | '.$array2['2']] = $array2;
}
// Get only array keys
$old_arr_key = array_keys($portal_old[0]);
$new_arr_key = array_keys($portal_new[0]);

// Result
$result = array_diff($new_arr_key,$old_arr_key);
print_r($result);

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