★ wanayoo — archive 1999 http://fr.php.net/manual/cs/function.array-intersect-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_fliparray_intersect>
view the version of this page
Last updated: Wed, 24 Nov 2004

array_intersect_assoc

(PHP 4 >= 4.3.0, PHP 5)

array_intersect_assoc -- Computes the intersection of arrays with additional index check

Description

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

array_intersect_assoc() returns an array containing all the values of array1 that are present in all the arguments. Note that the keys are used in the comparison unlike in array_intersect().

Příklad 1. array_intersect_assoc() example

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

$result_array will look like:

Array
(
    [a] => green
)

In our example you see that only the pair "a" => "green" is present in both arrays and thus is returned. The value "red" is not returned because in $array1 its key is 0 while the key of "red" in $array2 is 1.

The two values from the key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In otherwords a strict type check is executed so the string representation must be the same.

See also array_intersect(), array_diff() and array_diff_assoc().



add a note add a note User Contributed Notes
array_intersect_assoc
nleippe at integr8ted dot com
12-Feb-2004 01:46
A combination of the two functions [array_intersect() and array_intersect_assoc()] in order to obtain the intersection of two multidimensional arrays.

<?php
function array_intersect_assoc_recursive(&$arr1, &$arr2) {
   if (!
is_array($arr1) || !is_array($arr2)) {
       return
$arr1 == $arr2; // or === for strict type
  
}
  
$commonkeys = array_intersect(array_keys($arr1), array_keys($arr2));
  
$ret = array();
   foreach (
$commonkeys as $key) {
      
$ret[$key] =& array_intersect_assoc_recursive($arr1[$key], $arr2[$key]);
   }
   return
$ret;
}
?>

<array_fliparray_intersect>
 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: Tue Nov 30 05:17:37 2004 CET