Inverse of array_intersect ()

The arguments are as follows:

function foo(arr1, arr2, arr3, arr4 ...)

and the function should return an array of all elements from arr2, arr3, arr4 ... that do not exist in arr1.

Is there a built-in function for this? Or do I need to do this using foreach etc.? :)

+5
source share
3 answers

There is no built-in function that does exactly what you are asking for. array_diff()close, but not quite. So you have to collapse your own beautiful neat function or do something ugly:

array_diff( array_unique(
                array_merge(
                    array_values($arr2), 
                    array_values($arr3), 
                    array_values($arr4)
                )),
                $arr1 
           );

You can remove the call array_unique()if you want the values โ€‹โ€‹that appear several times in arrays to also be presented several times in your result.

array_values(), , , , .

, :

array_diff( array_merge( $arr2, $arr3, $arr4 ), $arr1 );
+10

array_diff, .

$result = array_diff (array_merge ($arr2, $arr3, $arr4), $arr1);

EDIT: Trott , . , , , .

+5

All Articles