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

search for in the

array_diff_key> <array_count_values
[edit] Last updated: Fri, 25 May 2012

view this page in

array_diff_assoc

(PHP 4 >= 4.3.0, PHP 5)

array_diff_assoc追加された添字の確認を含めて配列の差を計算する

説明

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

array1array2 と比較し、 その差を返します。array_diff() とは異なり、 配列のキーを用いて比較を行います。

パラメータ

array1

比較元の配列。

array2

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

...

さらに比較する対象となる配列。

返り値

array1 の要素のうち、 その他の配列のいずれにも含まれないものだけを残した配列を返します。

例1 array_diff_assoc() の例

この例で、"a" => "green" の組が両方の配列に現れており、 このため、この関数の出力には含まれていません。 これとは異なり、0 => "red" は出力の中にありますが、 これは、二番目の引数において "red"1 というキーを有しているためです。

<?php
$array1 
= array("a" => "green""b" => "brown""c" => "blue""red");
$array2 = array("a" => "green""yellow""red");
$result array_diff_assoc($array1$array2);
print_r($result);
?>

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

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

例2 array_diff_assoc() の例

key => value の組からの二つの値は、 (string) $elem1 === (string) $elem2 が成り立つ場合のみ等しいと見なされます。 言い替えると、厳密なチェックが行われるため、 文字列表現が同じである必要があります。

<?php
$array1 
= array(012);
$array2 = array("00""01""2");
$result array_diff_assoc($array1$array2);
print_r($result);
?>

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

Array
(
    [0] => 0
    [1] => 1
    )

注意

注意: この関数は、N 次元配列の一次元だけを調べます。もちろん、例えば array_diff_assoc($array1[0], $array2[0]); とすることにより、より深い次元でチェックを行うことも可能です。

参考



array_diff_key> <array_count_values
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes array_diff_assoc
news_yodpeirs at thoftware dot de 12-Jul-2011 04:25
A quite simple (yet not very efficient) way to compare the first level of arrays which have values that are not strings:
array_map('unserialize',array_diff_assoc(array_map('serialize',$arr1),array_map('serialize',$arr2)))
Might be useful for debugging (that's what I use it for).
jrajpu10 at gmail dot com 25-Oct-2008 12:57
array_diff_assoc can also be used to find the duplicates in an array

<?php
$arr
= array('1','2','3','4','3','2','5');
$uniques = array_unique($arr);
// array_diff will not work here, array_diff_assoc works as it takes the key // in account.
$dups = array_diff_assoc($arr, $uniques);

print_r($dups);
?>

Note: The index of the $dups is not in strict sequential order as expected by C programmer.
cedric at daneel dot net 21-May-2007 06:01
To diff between n-dimensional array, juste use this :

<?php
function array_diff_values($tab1, $tab2)
    {
   
$result = array();
    foreach(
$tab1 as $values) if(! in_array($values, $tab2)) $result[] = $values;
    return
$result;
    }
?>
contact at pascalopitz dot com 11-Apr-2007 04:14
The direction of the arguments does actually make a difference:

<?php
$a
= array(
   
'x' => 'x',
   
'y' => 'y',
   
'z' => 'z',
   
't' => 't',
);

$b = array(
   
'x' => 'x',
   
'y' => 'y',
   
'z' => 'z',
   
't' => 't',
   
'g' => 'g',
);

print_r(array_diff_assoc($a, $b));
print_r(array_diff_assoc($b, $a));
?>

echoes:

Array
(
)
Array
(
    [g] => g
)
chinello at gmail dot com 18-Mar-2007 10:33
The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level.  This not display any notices if a key don't exist and if error_reporting is set to E_ALL:

<?php
function array_diff_assoc_recursive($array1, $array2)
{
    foreach(
$array1 as $key => $value)
    {
        if(
is_array($value))
        {
              if(!isset(
$array2[$key]))
              {
                 
$difference[$key] = $value;
              }
              elseif(!
is_array($array2[$key]))
              {
                 
$difference[$key] = $value;
              }
              else
              {
                 
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                  if(
$new_diff != FALSE)
                  {
                       
$difference[$key] = $new_diff;
                  }
              }
          }
          elseif(!isset(
$array2[$key]) || $array2[$key] != $value)
          {
             
$difference[$key] = $value;
          }
    }
    return !isset(
$difference) ? 0 : $difference;
}
?>

[NOTE BY danbrown AT php DOT net: This is a combination of efforts from previous notes deleted.  Contributors included (Michael Johnson), (jochem AT iamjochem DAWT com), (sc1n AT yahoo DOT com), and (anders DOT carlsson AT mds DOT mdh DOT se).]
Alexander Podgorny 30-May-2006 02:30
NOTE: the diff_array also removes all the duplicate values that match to the values in the second array:

<?php
    $array1
= array("a","b","c","a","a");
   
$array2 = array("a");

   
$diff = array_diff($array1,$array2);

   
// yields: array("b","c") the duplicate "a" values are removed
?>
benjamin at moonfactory dot co dot jp 11-Jan-2005 09:56
Hi all,
For php versions < 4.3...

<?php
/**
 * array_diff_assoc for version < 4.3
 **/
if (!function_exists('array_diff_assoc'))
{
    function
array_diff_assoc($a1, $a2)
    {
        foreach(
$a1 as $key => $value)
        {
            if(isset(
$a2[$key]))
            {
                if((string)
$value !== (string) $a2[$key])
                {
                    
$r[$key] = $value;
                }
            }else
            {
               
$r[$key] = $value;
            }
        }
        return
$r ;
    }
}

?>
carl at thep dot lu dot se 09-May-2003 02:55
To unset elements in an array if you know the keys but not the values, you can do:

<?php
$a
= array("foo", "bar", "baz", "quux");
$b = array(1, 3); // Elements to get rid of

foreach($b as $e)
  unset(
$a[$e]);
?>

Of course this makes most sense if $b has many elements or is dynamically generated.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites