- var_dump: documentation ( source)
- array_replace_recursive: documentation ( source)
<?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('foo' => 'bar')), array());
var_dump(extend(array('bar' => 'baz')), array());