Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : array_diff_key

array_diff_key

Computes the difference of arrays using keys for comparison (PHP 5 >= 5.1.0)
array array_diff_key ( array array1, array array2 [, array ...] )

Example 239. array_diff_key() example

The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.

<?php
$array1
= array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_diff_key($array1, $array2));
?>

The above example will output:

array(2) {
 ["red"]=>
 int(2)
 ["purple"]=>
 int(4)
}

Code Examples / Notes » array_diff_key

ml

You may obtain this function with PEAR Package PHP_Compat (http://pear.php.net/package/PHP_Compat)
Then using such code is quite useful
<?php
if(!function_exists('array_diff_key')){
require_once 'PHP/Compat/Function/array_diff_key.php';
}
?>


ampf

Well, you could implement in the code something more powerfull:
http://www.php.net/manual/en/function.array-diff.php#31364


maxence

Seems to be a great function, especially for n-dimensions arrays. The only problem is that I cannot find it in php 5.0.3 and 5.0.4. Does it really exist ?! :(
[20:27:05][maxence@conurb] ~/test2/php-5.0.4$ grep PHP_FUNCTION * -r | grep -i array_diff_key
[20:27:09][maxence@conurb] ~/test2/php-5.0.4$


kwutzke @ somewhere in the net

PHP4 array_diff_key can be copied from the array_intersect_key implementation posted by some anonymous user on 2006-07-17. The only thing you have to do is to delete the '!' in the if and rename the function.

vlad_mustafin

One more alternative variant :)
<?
if (!function_exists('array_diff_key')) {
   function array_diff_key() {
       $argCount   = func_num_args();
       $diff_arg_prefix = 'diffArg';
       $diff_arg_names = array();
       for ($i=0; $i < $argCount; $i++) {
           $diff_arg_names[$i] = 'diffArg'.$i;
           $$diff_arg_names[$i] = array_keys((array)func_get_arg($i));
       }
       $diffArrString = '';
       if (!empty($diff_arg_names)) $diffArrString =  '$'.implode(', $', $diff_arg_names);
       eval("\$result = array_diff(".$diffArrString.");");
       return $result;
   }
}
?>


denis noessler

if (!function_exists('array_diff_key'))
{
/**
* Computes the difference of arrays using keys for comparison
*
* @param array $valuesBase Base elements for comparison, associative
* @param array $valuesComp[,..] Comparison elements, associative
*
* @param array Elements, not existing in comparison element, associative
*/
function array_diff_key()
{
$argCount   = func_num_args();
$argValues  = func_get_args();
$valuesDiff = array();

if ($argCount < 2)
{
return false;
}

foreach ($argValues as $argParam)
{
if (!is_array($argParam))
{
return false;
}
}

foreach ($argValues[0] as $valueKey => $valueData)
{
for ($i = 1; $i < $argCount; $i++)
{
if (isset($argValues[$i][$valueKey]))
{
continue 2;
}
}

$valuesDiff[$valueKey] = $valueData;
}

return $valuesDiff;
}
}


2ge

Hello, if you need diff key of n-dimensional arrays here is nice solution:
<?php
function n_array_diff ($a1, $a2) {
       foreach($a1 as $k => $v) {
        $r[$k] = is_array($v) ? n_array_diff($a1[$k], $a2[$k]) : array_diff_key($a1, $a2);
       }
       return $r;
}
?>
it will print everything, what is missing in $a2.


nicolas

Be aware that the last solution doesn't work if for any reason, two values are the same.

contact

after kwutzke's comment , here is a PHP4 array_diff_key fonction for those in need
   function PHP4_array_diff_key()
   {
       $arrs = func_get_args();
       $result = array_shift($arrs);
       foreach ($arrs as $array) {
           foreach ($result as $key => $v) {
               if (array_key_exists($key, $array)) {
                   unset($result[$key]);
               }
           }
       }
       return $result;
  }
works for me, enjoy.


eric dot broersma

<?php
function array_diff_key()
{
$args = func_get_args();
return array_flip(call_user_func_array('array_diff',
      array_map('array_flip',$args)));
}
?>


Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker