I needed a function to only remove the element the amount of times he appears in the second array. In other words, if you have Array(1, 1, 2) and Array(1), the return value should be Array(1, 2).
So I built this function right here:
<?php
function array_diff_once(){
if(($args = func_num_args()) < 2)
return false;
$arr1 = func_get_arg(0);
$arr2 = func_get_arg(1);
if(!is_array($arr1) || !is_array($arr2))
return false;
foreach($arr2 as $remove){
foreach($arr1 as $k=>$v){
if((string)$v === (string)$remove){ unset($arr1[$k]);
break; }
}
}
$c = $args;
while($c > 2){
$c--;
$arr1 = array_diff_once($arr1, func_get_arg($args-$c+1));
}
return $arr1;
}
$arr1 = Array("blue", "four"=>4, "color"=>"red", "blue", "green", "green", "name"=>"jon", "green");
$arr2 = Array("4", "red", "blue", "green");
print_r(array_diff_once($arr1, $arr2));
?>
This prints:
Array ( [1] => blue [3] => green [name] => jon [4] => green )
Note that it removes the elements left to right, opposite to what you might expect; in my case the order of elements had no importance. Fixing that would require a small variation.