PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_intersect_uassoc> <array_intersect_assoc
Last updated: Fri, 10 Oct 2008

view this page in

array_intersect_key

(PHP 5 >= 5.1.0)

array_intersect_keyキーを基準にして配列の共通項を計算する

説明

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

array_intersect_key() は、他の全ての引数に存在する array1 の値を全て有する配列を返します。

パラメータ

array1

値を調べるもととなる配列。

array2

値を比較する対象となる配列。

array

さらにそれ以外の配列。

返り値

array1 の値のうち、 すべての引数に存在するキーのものを含む連想配列を返します。

例1 array_intersect_key() の例

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

上の例の出力は以下となります。

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

この例では、両方の配列に存在するキーが 'blue''green' だけであり、それが返されていることが わかります。また、ふたつの配列の間で 'blue''green' に対応する値が違うことにも注意しましょう。 それでも一致していると判定されるのは、ただキーだけがチェックされているからです。 返される値は array1 のものです。

二つの要素は、 (string) $elem1 === (string) $elem2 の場合のみ等しいとみなされます。言い換えると、 文字列表現が同じ場合となります。



array_intersect_uassoc> <array_intersect_assoc
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
array_intersect_key
CBWhiz at gmail dot com
05-Jan-2008 07:04
I have found the following helpful:
<?PHP
function array_merge_default($default, $data) {
       
$intersect = array_intersect_key($data, $default); //Get data for which a default exists
       
$diff = array_diff_key($default, $data); //Get defaults which are not present in data
       
return $diff + $intersect; //Arrays have different keys, return the union of the two
}
?>
It's use is like both of the functions it uses, but keeps defaults and _only_ defaults. It's designed for key arrays, and i'm not sure how it will work on numeric indexed arrays.

Example:
<?PHP
$default
= array(
 
"one" => 1,
 
"two" => 2
);
$untrusted = array(
 
"one" => 42,
 
"three" => 3
);
var_dump(array_merge_default($default, $untrusted));

array(
2) {
  [
"two"]=>
 
int(2)
  [
"one"]=>
 
int(42)
}

?>
Rod Byrnes
06-May-2007 01:10
Here is a faster version than those shown below, with optimisation for the case when only two arrays are passed. In my tests with a 10000 item first array and a 5000 item second array (run 20 times) this function ran in 1.89 seconds compared with 2.66 for the version posted by dak. For a three array case, same as above but with the third array containing 3333 values, the timing is 3.25 for this version compared with 3.7 for dak's version.

<?php
if (!function_exists('array_intersect_key'))
{
  function
array_intersect_key($isec, $keys)
  {
   
$argc = func_num_args();
    if (
$argc > 2)
    {
      for (
$i = 1; !empty($isec) && $i < $argc; $i++)
      {
       
$arr = func_get_arg($i);
        foreach (
array_keys($isec) as $key)
        {
          if (!isset(
$arr[$key]))
          {
            unset(
$isec[$key]);
          }
        }
      }
      return
$isec;
    }
    else
    {
     
$res = array();
      foreach (
array_keys($isec) as $key)
      {
        if (isset(
$keys[$key]))
        {
         
$res[$key] = $isec[$key];
        }
      }
      return
$res;
    }
  }
}
?>
17-Jul-2006 09:31
Here it is a more obvious way to implement the function:

if (!function_exists('array_intersect_key')) {
    function array_intersect_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;
   }
}
Anton Backer
31-Mar-2006 04:49
Jesse: no, array_intersect_key does not accomplish the same thing as what you posted:

array_flip (array_intersect (array_flip ($a), array_flip ($b)))

because when the array is flipped, values become keys. having duplicate values is not a problem, but having duplicate keys is. array_flip resolves it by keeping only one of the duplicates and discarding the rest. by the time you start intersecting, you've already lost information.
dak
24-Jan-2006 01:31
A more efficient (and, I think, simpler) compatibility implementation:

<?php
if (!function_exists('array_intersect_key'))
{
    function
array_intersect_key ($isec, $arr2)
    {
       
$argc = func_num_args();
        
        for (
$i = 1; !empty($isec) && $i < $argc; $i++)
        {
            
$arr = func_get_arg($i);
            
             foreach (
$isec as $k =>& $v)
                 if (!isset(
$arr[$k]))
                     unset(
$isec[$k]);
        }
       
        return
$isec;
    }
}
?>
Silvio Ginter
23-Sep-2005 09:17
Based on the code posted by gaylord dot aulke at 100days.de
i wrote this one. This should implement this function in all versions equal or greater than PHP 4.0

function array_intersect_key($arr1, $arr2) {
    $res = array();
    foreach($arr1 as $key=>$value) {
        $push = true;
        for ($i = 1; $i < func_num_args(); $i++) {
            $actArray = func_get_arg($i);
            if (gettype($actArray) != 'array') return false;
            if (!array_key_exists($key, $actArray)) $push = false;
        }
        if ($push) $res[$key] = $arr1[$key];
    }
    return $res;
}
gaylord dot aulke at 100days.de
04-Jul-2005 08:04
I tried to use this function with PHP 5.0.4 under windows but the function does not seem to be implemented.
(Fatal error: Call to undefined function array_intersect_key())

This works as a workaround for 2 arrays at least:

function array_intersect_key($arr1, $arr2) {
  $res = array();
  foreach($arr1 as $key=>$value) {
    if(array_key_exists($key, $arr2)) $res[$key] = $arr1[$key];
  }
  return $res;
}
aidan at php dot net
30-May-2005 12:51
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat

array_intersect_uassoc> <array_intersect_assoc
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites