[jochem at iamjochem dawt com]
Here is a slightly enhanced version of Micheal Johnsons function.
This version accepts arguments in the same way as
array_diff_assoc (i.e. you can pass as many arrays as you want - any
arguments that are not arrays are ignored). If the first argument is not an array you automatically get empty array back:
The point of the function is to return all values in the first array
whose keys (only keys are checked!) are not present in any subsequently passed arrays.
[original post]
array_diff_assoc() requires that both the key and the value pairs match. To match based on keys only, try this function.
<?php
function array_diff_keys()
{
$args = func_get_args();
$res = $args[0];
if(!is_array($res)) {
return array();
}
for($i=1;$i<count($args);$i++) {
if(!is_array($args[$i])) {
continue;
}
foreach ($args[$i] as $key => $data) {
unset($res[$key]);
}
}
return $res;
}
// Example
$a = array('a' => '1', 'b' => '2', 'c' => '3');
$b = array('a' => '2', 'b' => '2', 'e' => '4');
// Yields array('a' => '1', 'c' => '3')
// Note that the 'a' index is not removed (as one might expect)
$c = array_diff_assoc($a, $b);
// Yields array('c' => '3')
$d = array_diff_keys($a, $b);
?>