How to delete both duplicate values from the array?

Asked

Viewed 193 times

1

I get two arrays

However, when merging them, the same values must be removed: in

array_unique( array_merge($array1, $array2) )

It is possible to remove duplicates, but I want to remove both duplicates.

Is there a simple method, or is looping even?

  • Give an example of the arrays' strature, will facilitate in understanding the problem and solution.

2 answers

2


You can remove duplicate items and their singular by diffraction the arrays twice $a with $b and of $b with $a.

The first call from array_diff() returns the goods: 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);

Exit:

Array
(
    [0] => 3
    [1] => 30
    [2] => 40
    [3] => 80
    [4] => 70
)

1

You can use array_intersect and update old arrays:

$array_antes = array_intersect($array_antes, $array_concatenado);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.