note 90362 deleted from function.array-diff by danbrown
| From: | danbrown@php.net | Date: | Fri, 17 Apr 2009 19:31:54 +0000 |
| Subject: | note 90362 deleted from function.array-diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153452@lists.php.net to get a copy of this message | ||
Note Submitter: p at rdus dot de
----
In response to the function suggested by ottodenn at gmail dot com:
You should probably also check if one of the arrays is empty. This one worked for me and ran fine in
a suite of unit tests:
function getArrayChanges($a1, $a2)
{
if (empty($a1) || empty($a2)) {
return !empty($a1) ? $a1 : $a2;
}
$ar = array();
foreach ($a2 as $k => $v) {
if (!is_array($v) || !is_array($a1[$k])) {
if ($v !== $a1[$k]) {
$ar[$k] = $v;
}
} else {
if ($arr = $this->getArrayChanges($a1[$k], $a2[$k])) {
$ar[$k] = $arr;
}
}
}
return $ar;
}