Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Last active December 20, 2018 11:18
Show Gist options
  • Save patrickmaciel/47e0fb73fddea873e4fac908b8d941a9 to your computer and use it in GitHub Desktop.
Save patrickmaciel/47e0fb73fddea873e4fac908b8d941a9 to your computer and use it in GitHub Desktop.
Compare arrays multidimensional #php
<?php
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach($array1 as $key => $value) {
if( is_array($value) ) {
if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if( !empty($new_diff) )
$difference[$key] = $new_diff;
}
} else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
$difference[$key] = $value;
}
}
return $difference;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment