note 39822 modified in function.array-intersect-assoc by aidan
| From: | aidan@php.net | Date: | Sun, 12 Sep 2004 13:28:22 +0000 |
| Subject: | note 39822 modified in function.array-intersect-assoc by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76522@lists.php.net to get a copy of this message | ||
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;
}
?>
--was--
In response to the first posting below, try this:
<?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;
}
?>
http://php.net/manual/en/function.array-intersect-assoc.php