3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_merge_recursive_distinct() { $arrays = func_get_args(); $base = array_shift($arrays); foreach ($arrays as $array) { reset($base); //important while (list($key, $value) = @each($array)) { if (is_array($value) && @is_array($base[$key])) { $base[$key] = self::array_merge_recursive_distinct($base[$key], $value); } else { $base[$key] = $value; } } } return $base; } $a = array( "exists" => 1, "doesnotexist" => 1, "arraytest" => array( "exists" => 1, "doesnotexist" => 1 ) ); $b = array( "exists" => 2, "arraytest" => array( "exists" => 2, ) ); $c = array_merge_recursive_distinct($a, $b); print_r($c);
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.30, 5.6.7 - 5.6.14
Fatal error: Cannot access self:: when no class scope is active in /in/RlNEo on line 11
Process exited with code 255.

preferences:
179.16 ms | 1395 KiB | 67 Q