There are two arrays that need to be compared and leave the difference between. There is the first array:

<pre>Array ( [0] => dubstep ) </pre> 

There is a second array

 <pre>Array ( [0] => dubstep [1] => black metal ) </pre> 

And the idea of ​​the difference would be black metal, but using the array_diff function, I get the result: <pre> Array () </ pre>

Why is that? How does it really work?

  • one
    There will be elements that are in the first array, but not in the others. That is, the first argument is special, and the rest are endless. - Oleg Arkhipov

1 answer 1

See the same documentation !

The elements of the first array that are not in the second (and subsequent) are returned. Essentially, this is a set difference, not a symmetric difference.


If you need a symmetric difference, do this:

 array_merge(array_diff($a1, $a2), array_diff($a2, $a1)); 
  • array_merge (array_diff ($ a1, $ a2), array_diff ($ a2, $ a1)); did not work if $ a1 came empty, I also did a check :) But I found this symmetry right after I received the first version of your answer :) - Tchort
  • Hmm, weird, but what exactly did not work? ideone.com/WkOAUU - VladD