How to delete both duplicate values from the array?

1

I get two arrays

But I need them to be merged, values that are equal are removed: in

array_unique( array_merge($array1, $array2) )

You can remove the duplicates, but I want to remove both duplicates.

Is there a simple method, or is it even looping?

    
asked by anonymous 15.08.2017 / 20:18

2 answers

2

You can remove duplicate and singular items by making the difference in arrays twice as much as $a with $b and $b with $a .

The first call of array_diff() returns the elements: 3, 30 and 40 and the second: 70 and 80

$a1 = array(1, 2, 3, 5, 30, 40,99);
$a2 = array(1, 2, 5, 80, 70, 99);

$n = array_merge(array_diff($a1, $a2) , array_diff($a2, $a1));

echo "<pre>";
print_r($n);

Output:

Array
(
    [0] => 3
    [1] => 30
    [2] => 40
    [3] => 80
    [4] => 70
)
    
15.08.2017 / 20:25
1

You can use array_intersect and update the old arrays:

$array_antes = array_intersect($array_antes, $array_concatenado);
    
15.08.2017 / 20:25