查找数组匹配并删除元素

时间:2014-02-10 10:37:39

标签: php arrays match

给出两个数组:

$arr1 = (1, 0, 9);
$arr2 = (8, 0, 2, 9, 4);

如何删除$arr2中包含的$arr1中的元素?
这将返回$arr3 = (8, 2, 4);

我试过这个:

<?php
foreach($arr1 as $item)
{
       if(in_array($item, $arr2))
       {
          # This is were I lose it, I've found a match then what?
       }
}
?>

1 个答案:

答案 0 :(得分:1)

array_diff正是您要找的

看看here

$arr1 = array(1, 0, 9);
$arr2 = array(8, 0, 2, 9, 4);

echo print_r(array_diff($arr2,$arr1),true);

>> Array ( [0] => 8 [2] => 2 [4] => 4 )