Skip to content

Instantly share code, notes, and snippets.

@d9k
Last active August 29, 2015 14:16
Show Gist options
  • Save d9k/fec0bc20ab87e749a9c1 to your computer and use it in GitHub Desktop.
Save d9k/fec0bc20ab87e749a9c1 to your computer and use it in GitHub Desktop.
array_replace_recursive for php 5.3 and lower
if (!function_exists('array_replace_recursive'))
{
function array_replace_recursive___recurse($array, $array1)
{
foreach ($array1 as $key => $value)
{
// create new key in $array, if it is empty or not an array
if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key])))
{
$array[$key] = array();
}
// overwrite the value in the base array
if (is_array($value))
{
$value = array_replace_recursive___recurse($array[$key], $value);
}
$array[$key] = $value;
}
return $array;
}
function array_replace_recursive($array, $array1)
{
// handle the arguments, merge one by one
$args = func_get_args();
$array = $args[0];
if (!is_array($array))
{
return $array;
}
for ($i = 1; $i < count($args); $i++)
{
if (is_array($args[$i]))
{
$array = array_replace_recursive___recurse($array, $args[$i]);
}
}
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment