3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Tools{ /** * Recursive version of array_diff_assoc * Returns everything from $a that is not in $b or the other arguments * * @param $a The array to compare from * @param $b An array to compare against * @param ... More arrays to compare against * * @return An array with everything from $a that not in $b or the others */ public static function array_diff_assoc_recursive($a, $b){ // Get all of the "compare against" arrays $b = array_slice(func_get_args(), 1); // Initial return value $ret = array(); // Loop over the "to" array and compare with the others foreach($a as $key=>$val){ // We should compare type first $aType = gettype($val); // If it's an array, we recurse, otherwise we just compare with "===" $args = $aType === 'array' ? array($val) : true; // Let's see what we have to compare to foreach($b as $x){ // If the key doesn't exist or the type is different, // then it's different, and our work here is done if(!array_key_exists($key, $x) || $aType !== gettype($x[$key])){ $ret[$key] = $val; continue 2; } // If we are working with arrays, then we recurse if($aType === 'array'){ $args[] = $x[$key]; } // Otherwise we just compare else{ $args = $args && $val === $x[$key]; } } // This is where we call ourselves with all of the arrays we got passed if($aType === 'array'){ $comp = call_user_func_array(array(get_called_class(), 'array_diff_assoc_recursive'), $args); // An empty array means we are equal :-) if(count($comp) > 0){ $ret[$key] = $comp; } } // If the values don't match, then we found a difference elseif(!$args){ $ret[$key] = $val; } } return $ret; } } $a = [[1, 2, 3], 4, [5, 6 ], 8, 10]; $b = [[ 2, 3], 4, [5, 6 ] ]; $c = [[1, 2 ], 4, [5, 6, 7] ]; $d = [[ 2 ], 4, [5, 6, 7], 8, 9 ]; var_export(Tools::array_diff_assoc_recursive($a, $b, $c, $d));
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
array ( 0 => array ( 0 => 1, 1 => 2, 2 => 3, ), 3 => 8, 4 => 10, )

preferences:
137.7 ms | 407 KiB | 5 Q