array_diff_assoc

(PHP 4 >= 4.3.0, PHP 5)

array_diff_assocВычисляет расхождение массивов с дополнительной проверкой индекса

Описание

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

Сравнивает array1 с array2 и возвращает разницу. В отличие от array_diff(), ключи также участвуют в сравнении.

Список параметров

array1

Исходный массив

array2

Массив, с которым идет сравнение

...

Дополнительные массивы, с которыми осуществляется сравнение

Возвращаемые значения

Возвращает array, содержащие все значения из массива 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() - Вычислить расхождение массивов
  • array_intersect() - Вычисляет схождение массивов
  • array_intersect_assoc() - Вычисляет схождение массивов с дополнительной проверкой индекса

Коментарии

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(13); // 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.
2003-05-09 05:55:19
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
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 ;
    }
}

?>
2005-01-11 23:56:52
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
Автор:
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 
?>
2006-05-30 17:30:27
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
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) ? $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).]
2007-03-19 00:33:31
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
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
)
2007-04-11 07:14:59
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
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;
    }
?>
2007-05-21 09:01:12
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
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.
2008-10-25 15:57:20
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
I've worked on array_diff_assoc_recursive() mentioned by chinello at gmail dot com and I think it might be worth mentioning here. I wrote a dozen test cases and it seems to be holding up pretty well.

<?php
// dwarven Differences:
// * Replaced isset() with array_key_exists() to account for keys with null contents

// 55 dot php at imars dot com Differences:
// Key differences:
// * Removed redundant test;
// * Returns false bool on exact match (not zero integer);
// * Use type-precise comparison "!==" instead of loose "!=";
// * Detect when $array2 contains extraneous elements;
// * Returns "before" and "after" instead of only "before" arrays on mismatch.

function array_compare($array1$array2) {
   
$diff false;
   
// Left-to-right
   
foreach ($array1 as $key => $value) {
        if (!
array_key_exists($key,$array2)) {
           
$diff[0][$key] = $value;
        } elseif (
is_array($value)) {
             if (!
is_array($array2[$key])) {
                   
$diff[0][$key] = $value;
                   
$diff[1][$key] = $array2[$key];
             } else {
                   
$new array_compare($value$array2[$key]);
                    if (
$new !== false) {
                         if (isset(
$new[0])) $diff[0][$key] = $new[0];
                         if (isset(
$new[1])) $diff[1][$key] = $new[1];
                    };
             };
        } elseif (
$array2[$key] !== $value) {
             
$diff[0][$key] = $value;
             
$diff[1][$key] = $array2[$key];
        };
 };
 
// Right-to-left
 
foreach ($array2 as $key => $value) {
        if (!
array_key_exists($key,$array1)) {
             
$diff[1][$key] = $value;
        };
       
// No direct comparsion because matching keys were compared in the
        // left-to-right loop earlier, recursively.
 
};
 return 
$diff;
};
?>

Note that to catch small type differences, you need to use var_dump() rather than print_r(). For example:

<?php
first 
= Array( 'first' => 'second''third' => 'fourth',
 
'fifth' => Array( 'oui' => 'yes''non' => 'no' ),
 
'sixth' => Array( 342)
);
$second = Array( 'first' => 'second''third' => 'fourth',
 
'fifth' => Array( 'oui' => 'yes''non' => 'no' ),
 
'sixth' => Array( 34'2')
);
$diff array_compare($first$second);
if (
$diff === false) echo "MATCH\n";
else 
var_dump($diff);
?>

The above would immediately show that element "sixth", index 2, is int(2) in $first but string(1)"2" in $second. As per the original function, order for associative arrays does not matter, and of course it does with lists.

[EDIT: Credits to php dot notes at dwarven dot co dot uk]
2009-03-17 09:09:14
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html
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).
2011-07-12 07:25:05
http://php5.kiev.ua/manual/ru/function.array-diff-assoc.html

    Поддержать сайт на родительском проекте КГБ