Telen Stanley Telen Stanley - 24 days ago 7
PHP Question

how to filter elements from an array with respect to another array?

array('5','6','3')

array('3','2','1','5','9','0','6')


I need the elements of 2nd array removing the elements matching from the first array. ie.
array('2','1','9','0')


please help.

Answer

you can use array_diff

<?php 
$array1=array('5','6','3');
$array2=array('3','2','1','5','9','0','6');
$diff = array_diff($array2, $array1);

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

output:

Array
(
    [1] => 2
    [2] => 1
    [4] => 9
    [5] => 0
)