Getting unique values from 2 arrays

2020-01-29 07:26发布

问题:

I have 2 arrays that I'm trying to get the unique values only from them. So I'm not just trying to remove duplicates, I'm actually trying to remove both duplicates.

So if I'm getting the 2 arrays like this:

$array1 = array();
$array2 = array();

foreach($values1 as $value1){ //output: $array1 = 10, 15, 20, 25;
    $array1[] = $value1;
}   

foreach($values2 as $value2){ //output: $array2 = 10, 15, 100, 150;
    $array2[] = $value2;
}

The final output I'm looking for is

$output = 20, 25, 100, 150;

Any neat way to getting this done?

回答1:

The other answers are on the right track, but array_diff only works in one direction -- ie. it returns the values that exist in the first array given that aren't in any others.

What you want to do is get the difference in both directions and then merge the differences together:

$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$output = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
// $output will be (20, 25, 100, 150);


回答2:

Not to detract from Daniel Vandersluis's answer, but to add to it...

What you're looking for is basically an XOR operation of the arrays. To that end, "merlinyoda at dorproject dot net" provided the following routine, in a comment on http://php.net/manual/en/function.array-diff.php :

<?php
function array_xor ($array_a, $array_b) {
    $union_array = array_merge($array_a, $array_b);
    $intersect_array = array_intersect($array_a, $array_b);
    return array_diff($union_array, $intersect_array)
}
?>

This function takes a different approach to calculating the XOR.



回答3:

Here is the code to do it. It may be able to be optimized, but you get the idea:

$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);

$new_array = array();
foreach($array1 as $value) {
    if(!in_array($value, $array2)) {
        array_push($new_array, $value);
    }
}
foreach($array2 as $value) {
    if(!in_array($value, $array1)) {
        array_push($new_array, $value);
    }
}

print_r($new_array);

To use array_diff, you would have to do:

$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);

$out1 = array_diff($array1, $array2);
$out2 = array_diff($array2, $array1);
$output = array_merge($out1, $out2);

print_r($output);


回答4:

Try following code it works fine for numbers,String and every condition

The following Logic will only works for numbers.

$array1 = array(10, 15, 20, 25);
$array2 = array(10, 15, 100, 150);
$output = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));

The following logic will work fine for any condition

$a1 = array('1@gmail.com'); 
$a2 = array('1@gmail.com','2@gmail.com');
$new_array=array_merge($a1,$a2);
$unique=array_unique($new_array);

Thanks



回答5:

Another good solution is this:

$array1 = array(10, 15, 20, 25);

$array2 = array(10, 15, 100, 150);

$output = array_diff(array_merge($array1, $array2), array_intersect($array1, $array2));

// $output will be (20, 25, 100, 150);



回答6:

The array_diff() (manual) function can be used to find the difference between two arrays:

$array1 = array(10, 20, 40, 80);
$array2 = array(10, 20, 100, 200);
$diff = array_diff($array1, $array2);
// $diff = array(40, 80, 100, 200);

You can pass as many arrays as you want to the function, it is not just limited to two arrays.



回答7:

see array_diff()

$output = array_diff($array1, $array2);


标签: php arrays