I have two multidimensional arrays and I want the difference. For eg. I have taken two-dimensional two arrays below
$array1 = Array (
[a1] => Array
I know this thread is kind of old, however I ran into a few problems with the original solution. So here is my solution of the problem.
private function array_diff_recursive($array1, $array2){
$result = [];
foreach($array1 as $key => $val) {
if(array_key_exists($key, $array2)){
if(is_array($val) || is_array($array2[$key])) {
if (false === is_array($val) || false === is_array($array2[$key])) {
$result[$key] = $val;
} else {
$result[$key] = $this->array_diff_recursive($val, $array2[$key]);
if (sizeof($result[$key]) === 0) {
unset($result[$key]);
}
}
}
} else {
$result[$key] = $val;
}
}
return $result;
}
Problems Encountered / Fixed