lok
lok

Reputation: 415

Getting unique values from 2 arrays

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?

Upvotes: 18

Views: 39516

Answers (8)

Nithi2023
Nithi2023

Reputation: 433

You can use array_merge and array_unique like below:

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

This is simple and easy to understand.

Upvotes: 0

Rohit Joshi
Rohit Joshi

Reputation: 61

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('[email protected]'); 
$a2 = array('[email protected]','[email protected]');
$new_array=array_merge($a1,$a2);
$unique=array_unique($new_array);

Thanks

Upvotes: 5

Saverio Molinaro
Saverio Molinaro

Reputation: 21

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

Upvotes: 2

Mattias Andersson
Mattias Andersson

Reputation: 2341

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.

Upvotes: 8

Chuck Burgess
Chuck Burgess

Reputation: 11574

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

Upvotes: 4

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94153

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

Upvotes: 45

Freyja
Freyja

Reputation: 40804

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.

Upvotes: 1

Sergey Eremin
Sergey Eremin

Reputation: 11080

see array_diff()

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

Upvotes: -2

Related Questions