search for in the  
<array_diff_assocarray_diff_uassoc>
Last updated: Thu, 19 May 2005

array_diff_key

(PHP 5)

array_diff_key -- Computes the difference of arrays using keys for comparison

Description

array array_diff_key ( array array1, array array2 [, array ...] )

array_diff_key() returns an array containing all the values of array1 that have keys that are not present in any of the other arguments. Note that the associativity is preserved. This function is like array_diff() except the comparison is done on the keys instead of the values.

Example 1. array_diff_key() example

<?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)
}

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.

Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]);.

See also array_diff(), array_udiff() array_diff_assoc(), array_diff_uassoc(), array_udiff_assoc(), array_udiff_uassoc(), array_diff_ukey(), array_intersect(), array_intersect_assoc(), array_intersect_uassoc(), array_intersect_key() and array_intersect_ukey().



User Contributed Notes
array_diff_key
nicolas at goinf dot com
14-Apr-2005 10:28
Be aware that the last solution doesn't work if for any reason, two values are the same.
eric dot broersma at phil dot uu dot nl
05-Mar-2005 08:58
<?php
function array_diff_key()
{
  
$args = func_get_args();
   return
array_flip(call_user_func_array('array_diff',
          
array_map('array_flip',$args)));
}
?>
denis noessler
23-Nov-2004 06:07
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;
   }
}

<array_diff_assocarray_diff_uassoc>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT