International PHP Conference Munich 2025

Voting

: zero plus seven?
(Example: nine)

The Note You're Voting On

sjungwirth at matrix-consultants dot com
17 years ago
I needed something a little different where maybe even the keys in multidimensional arrays don't match up. Setting $assoc to false will cause only to check for missing keys, otherwise it compares values as well. This was also based on '2ge at 2ge dot us' function

<?php

function n_array_diff_assoc ($a1, $a2, $assoc=true) {
$r = array();
if(
is_array(current($a1))):
foreach(
$a1 as $k => $v):
if(isset(
$a2[$k])):
$diff = n_array_diff($a1[$k], $a2[$k], $assoc);
if (!empty(
$diff)):
$r[$k] = $diff;
endif;
else:
$r[$k] = $v;
endif;
endforeach;
else:
$r = $assoc ? array_diff_assoc($a1, $a2) : array_diff_key($a1, $a2);
endif;
return
$r;
}
?>

<< Back to user notes page

To Top