★ wanayoo — archive 1999 http://fr.php.net/manual/nl/function.array-diff-assoc.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<array_count_valuesarray_diff_uassoc>
view the version of this page
Last updated: Wed, 24 Nov 2004

array_diff_assoc

(PHP 4 >= 4.3.0, PHP 5)

array_diff_assoc -- Berekent het verschil tussen arrays met een toegevoegde index check

Beschrijving

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

array_diff_assoc() geeft een array terug met daarin alle waarden van array1 die niet aanwezig zijn in een van de andere argumenten. Let erop dat de keys worden vergelijken, in tegenstelling tot array_diff().

Voorbeeld 1. array_diff_assoc() voorbeeld

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

/* Het resultaat is:
Array
(
   [b] => brown
   [c] => blue
   [0] => red
)
*/
?>

In ons voorbeeld hierboven zie je dat het paar "a" => "green" is aanwezig in beide arrays, en het zit dus niet in de output van de functie. Daarentegen is het paar 0 => "red" wel in de output omdat het tweede argument "red" een key heeft die 1 is.

Twee waarden van key => value paren worden gezien als gelijk als en alleen als (string) $elem1 === (string) $elem2 . Er wordt dus strict gecheckt of de string representaties hetzelfde zijn.

Opmerking: Let erop dat deze functie slechts een dimensie van een n-dimensionale array checkt. Het is uiteraard mogelijk om diepere dimensies te checken door via bijvoorbeeld array_diff_assoc($array1[0], $array2[0]);.

Zie ook array_diff(), array_intersect(), en array_intersect_assoc().



add a note add a note User Contributed Notes
array_diff_assoc
aidan at php dot net
09-Jun-2004 03: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 08: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 09: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 11: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_count_valuesarray_diff_uassoc>
 Last updated: Wed, 24 Nov 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Nov 27 05:15:21 2004 CET