Merging arrays recursively some problem about existing keys, so that i wrote the function above like this:
function merge($array0, $array1) {
// Result
$merged = array();
foreach (func_get_args() as $array) {
// Check incoming argument is array
if (is_array($array)) {
foreach ($array as $key => $value) {
// Check there is an array with the key: $key
if (isset($merged[$key])) {
// Check the value is array
if (is_array($value)) {
// So we must merge current value with the existing array
$merged[$key] = call_user_func_array(__FUNCTION__, $merged[$key], $value);
} else {
if (!is_array($merged[$key])) {
// If the existing array with the key: $key not an array
// We make it an array
$merged[$key] = array($merged[$key]);
}
// And add the current value
$merged[$key][] = $value;
}
} else {
// If not exists make the array
$merged[$key] = $value;
}
}
}
}
return $merged;
}