Posts Tagged ‘array_intersect’

PHP code that returns array elements from array2 that are not present in array1

Saturday, February 28th, 2009
<?php
// PHP code that returns array elements from array2 that are not present in array1

$array1 = array(1, 2);
$array2 = array(1, 2, 3, 4);
$result = array_diff($array2, array_intersect($array1, $array2));

/*
// Result
Array
(
[2] => 3
[3] => 4
)
*/

?>