I have two multidimensional arrays that I want to compare. This is how they look like. I want to get the difference. I tried array diff but it doesn't seem to work. Heres is my code
Array1
(
[0] => Array
(
[name] => john
[surname] => elvis
[idnumber] => 01148015
)
[1] => Array
(
[name] => sammy
[surname] => dwayne
[idnumber] => 01148046
)
)
Array2
(
[0] => Array
(
[name] => john
[surname] => elvis
[idnumber] => 01148015
)
)
$difference = array_diff($Array1, $Array2);
print_r($difference);
Try this:
You can also see here: http://php.net/manual/en/function.array-diff-assoc.php#111675
array_diff_assoc_recursive($a1, $a2);
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) ? 0 : $difference;
}
Use array_intersect() instead:
$result = array_intersect($array1, $array2);