Recursive implementation accepting multiple n-level-arrays as parameters:
<?php
function recursiveDiff() {
$arrs = func_get_args();
$first = array_shift($arrs);
$diff = [];
foreach($first as $key => $value) {
$values = array_map(function($arr) use($key){
if (is_array($arr) && !array_key_exists($key, $arr))
return null;
return $arr[$key];
}, $arrs);
if (in_array($value, $values))
continue;
if (is_array($value)) {
array_unshift($values, $value);
$diff[$key] = call_user_func_array(__FUNCTION__, $values);
continue;
}
$diff[$key] = $first[$key];
}
return $diff;
}
?>
The other attempt was cleaner but didn't work for all cases.