3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function extend($base, $replacements) { $base = !is_array($base) ? array() : $base; $replacements = !is_array($replacements) ? array() : $replacements; $ret = array(); foreach ($base as $key => $val) { if (is_int($key)) { // value without key $ret[$val] = null; } else { $ret[$key] = $val; } } return array_replace_recursive($ret, $replacements); } var_dump(extend(array('foo' => 'bar'), array('bar' => 'baz'))); var_dump(extend(array('foo' => 'bar'), array('foo' => 'foo'))); var_dump(extend(array('bar' => 'baz'), array('bar' => 'bar'))); var_dump(extend(array('bar' => 'baz'))); var_dump(extend(array('bar' => 'baz')));
based on IIWdf
Output for 7.1.0 - 7.2.0
array(2) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "baz" } array(1) { ["foo"]=> string(3) "foo" } array(1) { ["bar"]=> string(3) "bar" } Fatal error: Uncaught ArgumentCountError: Too few arguments to function extend(), 1 passed in /in/JHEUh on line 22 and exactly 2 expected in /in/JHEUh:3 Stack trace: #0 /in/JHEUh(22): extend(Array) #1 {main} thrown in /in/JHEUh on line 3
Process exited with code 255.
Output for 5.3.22 - 7.0.20
array(2) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "baz" } array(1) { ["foo"]=> string(3) "foo" } array(1) { ["bar"]=> string(3) "bar" } Warning: Missing argument 2 for extend(), called in /in/JHEUh on line 22 and defined in /in/JHEUh on line 3 Notice: Undefined variable: replacements in /in/JHEUh on line 5 array(1) { ["bar"]=> string(3) "baz" } Warning: Missing argument 2 for extend(), called in /in/JHEUh on line 23 and defined in /in/JHEUh on line 3 Notice: Undefined variable: replacements in /in/JHEUh on line 5 array(1) { ["bar"]=> string(3) "baz" }