Created
March 16, 2015 10:39
-
-
Save probil/01cafcc29289cfbf054e to your computer and use it in GitHub Desktop.
Return recursive difference of 2 multidimensional arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function array_diff_assoc_recursive($array1, $array2) | |
{ | |
foreach($array1 as $key => $value) { | |
if(is_array($value)) { | |
if(!isset($array2[$key])) { | |
$difference[$key] = $value; | |
} | |
elseif(!is_array($array2[$key])) { | |
$difference[$key] = $value; | |
} | |
else{ | |
$new_diff = array_diff_assoc_recursive($value, $array2[$key]); | |
if($new_diff != FALSE) { | |
$difference[$key] = $new_diff; | |
} | |
} | |
} | |
elseif(!isset($array2[$key]) || $array2[$key] != $value) { | |
$difference[$key] = $value; | |
} | |
} | |
return !isset($difference) ? false : $difference; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment