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

array_intersect_key

(PHP 5)

array_intersect_key -- Computes the intersection of arrays using keys for comparison

Description

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

array_intersect_key() returns an array containing all the values of array1 which have matching keys that are present in all the arguments.

Example 1. array_intersect_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_intersect_key($array1, $array2));
?>

The above example will output:

array(2) {
  ["blue"]=>
  int(1)
  ["green"]=>
  int(3)
})

In our example you see that only the keys 'blue' and 'green' are present in both arrays and thus returned. Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array1.

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.

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



User Contributed Notes
array_intersect_key
bishop
02-Dec-2004 03:26
anarcat's array_intersect_key() implementation checks for key presence using isset() rather than array_key_exists(). Hence:

<?php
$a
= array ('foo' => 'bar');
$b = array ('foo' => null);
var_dump(array_intersect_key($a, $b));
?>

returns an empty array (ie, no matching keys), which is probably not right.  Also, the documented function signature takes two parameters, minimum, while anarcat's accepted one.

Consider this implementation instead:

<?php
function array_intersect_key() {
  
$numArgs = func_num_args();
   if (
2 <= $numArgs) {
      
$arrays =& func_get_args();
       for (
$idx = 0; $idx < $numArgs; $idx++) {
           if (!
is_array($arrays[$idx])) {
              
trigger_error('Parameter ' . ($idx+1) . ' is not an array', E_USER_ERROR);
               return
false;
           }
       }

       foreach (
$arrays[0] as $key => $val) {
           for (
$idx = 1; $idx < $numArgs; $idx++) {
               if (!
array_key_exists($key, $arrays[$idx])) {
                   unset(
$arrays[0][$key]);
               }
           }
       }

       return
$arrays[0];
   }

  
trigger_error('Not enough parameters; two arrays expected', E_USER_ERROR);
   return
false;
}
?>
anarcat at YOUKNOWHATODOWITISanarcat dot ath dot cx
22-Oct-2004 03:07
Behold!

An implementation of this function for PHP < CVS!

<?php

if (!function_exists("array_intersect_key")) {
 
/**
   * Computes the intersection of arrays using keys for comparison
   *
   * implementation of PHP' CVS array_intersect_key()
   *
   * array_intersect_key() returns an array containing all the values
   * of array1 which have matching keys that are present in all the
   * arguments.
   *
   * might not be exactly equivalent with php.net/array_intersect_key
   * since we do not use === for comparison.
   *
   * will trigger an warning and return FALSE if one of the arguments
   * is not an array or if less than one argument is given.
   *
   * @see http://php.net/array_intersect_key
   *
   * @author: The Anarcat
   * @license: public domain
   */
 
function array_intersect_key() {
  
$arrays =& func_get_args();
  
// if only one array is given as argument, just return it
  
if (count($arrays) == 1) return $arrays;
   elseif (
count($arrays) < 1) {
    
trigger_error("not enough arguments given, ".
                  
count($arrays) ." given, > 1 needed", E_USER_WARNING);
     return
FALSE;
   }
  
$array1 = array_shift($arrays);
   foreach (
$array1 as $key => $val) {
     for (
$i = 0; $i < count($arrays); $i++) {
      
$array =& $arrays[$i];
       if (!
is_array($array)) {
        
trigger_error("argument $i is not an array", E_USER_WARNING);
         return
FALSE;
       }
       if (!isset(
$array[$key])) {
         unset(
$array1[$key]);
       }
     }
   }
   return
$array1;
  }
}

?>

<array_intersect_assocarray_intersect_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