note 111707 deleted from function.array-intersect-assoc by aharvey
| From: | aharvey@php.net | Date: | Wed, 20 Mar 2013 18:49:34 +0000 |
| Subject: | note 111707 deleted from function.array-intersect-assoc by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-194140@lists.php.net to get a copy of this message | ||
Note Submitter: g dot kuizinas at anuary dot com
----
Another implementation of array_intersect_recursive.
<?php
/**
* @author Gajus Kuizinas <g.kuizinas@anuary.com>
* @version 1.0.0 (2013 03 19)
*/
function array_intersect_recursive (array $arr1, array $arr2) {
$return = [];
$common_keys = array_intersect(array_keys($arr1), array_keys($arr2));
foreach ($common_keys as $key) {
if (is_array($arr1[$key]) && is_array($arr2[$key])) {
$intersection = array_intersect_recursive ($arr1[$key], $arr2[$key]);
if ($intersection) {
$return[$key] = $intersection;
}
} else if ($arr1[$key] === $arr2[$key]) {
$return[$key] = $arr1[$key];
}
}
if (func_num_args() > 2) {
$arguments = func_get_args();
array_splice($arguments, 0, 2, [$return]);
$return = call_user_func_array('ay\array_intersect_recursive', $arguments);
}
return $return;
}
?>
If you are looking at this in 2020, you may seek for an updated version either at https://github.com/gajus/ay-framework or https://gist.github.com/gajus/ef658f04adaec16129a4.