Subtraction Of Arrays – How to get out of an array A only items that are not in array b, thus subtracting from array A all items of array B.
PHP:
We can try on our own:
<?php function SubtractionOfArrays($vectorA,$vectorB) { $countA=count($vectorA); $countB=count($vectorB); $numberOfElementsCaught=0; for($i=0;$i<$countA;$i++) { for($j=0;$j<$countB;$j++) { if($vectorA[$i]==$vectorB[$j]) $numberOfElementsCaught+=1; } if($numberOfElementsCaught==0) $new_array[]=$vectorA[$i]; else $numberOfElementsCaught=0; } return $new_array; } $result = SubtractionOfArrays($array1, $array2); print_r($result); ?>
Or we can use the simple provided array_diff:
<?php $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $resultSimple= array_diff($array1, $array2); print_r($resultSimple); ?>