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

search for in the

array_diff_key> <array_count_values
Last updated: Sun, 25 Nov 2007

view this page in

array_diff_assoc

(PHP 4 >= 4.3.0, PHP 5)

array_diff_assoc — Vypočíta rozdiel polí s dodatočným overením indexov

Popis

array array_diff_assoc ( array $pole1 , array $pole2 [, array $ ... ] )

array_diff_assoc() vracia pole obsahujúce všetky hodnoty od pole1 , ktoré sa nenachádzajú v žiadnom z ďalších argumentov. Všimnite si, že kľúče sa používajú v porovnávaní na rozdiel od array_diff().

Example#1 array_diff_assoc() príklad

<?php
$pole1 
= array ("a" => "zelena""b" => "hneda""c" => "modra""cervena");
$pole2 = array ("a" => "zelena""zlta""cervena");
$vysledok array_diff_assoc ($pole1$pole2);
print_r($vysledok);
?>

Výsledkom je:

Array
(
    [b] => hneda
    [c] => modra
    [0] => cervena
)

V našom príklade vidíte, že pár "a" => "zelena" sa nachádza v oboch poliach a teda nie je výstupom z funkcie. Na rozdiel od tohto, pár 0 => "cervena" je výstupom, pretože v druhom argumente "cervena" má kľúč, ktorý je 1.

Dve hodnoty z páru key => value sa považujú za rovnaké iba ak (string) $elem1 === (string) $elem2 . Inými slovami, koná sa striktné porovnávanie, takže reprezentácie reťazca musia byť rovnaké.

Note: Prosím všimnite si, že táto funkcia kontroluje iba jednu dimenziu n-dimezionálneho poľa. Samozrejme môžte kontrolovať aj hlbšie dimezie použitím, napr., array_diff_assoc($pole1[0], $pole2[0]);.

Tiež pozri array_diff(), array_intersect() a array_intersect_assoc().



array_diff_key> <array_count_values
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
array_diff_assoc
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 :

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:

<?
$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 11:33
A small modification for array_diff_assoc_recursive from 'sc1n at yahoo dot com' , to 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;
}
?>
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:

<?
    $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 ;
    }
}

Hope there's no bug,
cheers
?>
aidan at php dot net
09-Jun-2004 06:34
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
Michael Johnson
26-Oct-2003 12:20
[jochem at iamjochem dawt com]
Here is a slightly enhanced version of Micheal Johnsons function.
This version accepts arguments in the same way as
array_diff_assoc (i.e. you can pass as many arrays as you want - any
arguments that are not arrays are ignored). If the first argument is not an array you automatically get empty array back:

The point of the function is to return all values in the first array
whose keys (only keys are checked!) are not present in any subsequently passed arrays.

[original post]
array_diff_assoc() requires that both the key and the value pairs match. To match based on keys only, try this function.

<?php
function array_diff_keys()
{
   
$args = func_get_args();

   
$res = $args[0];
    if(!
is_array($res)) {
        return array();
    }

    for(
$i=1;$i<count($args);$i++) {
        if(!
is_array($args[$i])) {
            continue;
        }
        foreach (
$args[$i] as $key => $data) {
            unset(
$res[$key]);
        }
    }
    return
$res;
}

// Example
$a = array('a' => '1', 'b' => '2', 'c' => '3');
$b = array('a' => '2', 'b' => '2', 'e' => '4');

// Yields array('a' => '1', 'c' => '3')
// Note that the 'a' index is not removed (as one might expect)
$c = array_diff_assoc($a, $b);

// Yields array('c' => '3')
$d = array_diff_keys($a, $b);
?>
sc1n at yahoo dot com
11-Jul-2003 12:10
[anders dot carlsson at mds dot mdh dot se]
The user contributed array_diff_assoc_recursive function is good except for the original array_diff_assoc always (?) returns an array.

Therefore I propose that $difference is initially set to an empty array (and the array is always returned), and the comparison against FALSE is replaced by count($new_diff). At least that's the modifications I made to run it they way my code expects.

[original post]
The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level.  (Forgive me if the braces do not line up, the note script did not like my tabs, and gave me trouble on some spaces.)

<?php
function array_diff_assoc_recursive($array1, $array2)
{
     foreach(
$array1 as $key => $value)
     {
          if(
is_array($value))
          {
               if(!
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;
}
?>
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.

array_diff_key> <array_count_values
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites