This method can be thought of as a hybrid between PHP's `array_merge()`
and `array_merge_recursive()`. The difference to the two is that if an
array key contains another array then the function behaves recursive
(unlike `array_merge()`) but does not do if for keys containing strings
(unlike `array_merge_recursive()`). Please note: This function will work
with an unlimited amount of arguments and typecasts non-array parameters
into arrays.
Parameters
- array $array1 The base array.
- array $array2 The array to be merged on top of the base array.
Returns
array Merged array of all passed params.Source
public static function merge(array $array1, array $array2) {
$args = array($array1, $array2);
if (!$array1 || !$array2) {
return $array1 ?: $array2;
}
$result = (array) current($args);
while (($arg = next($args)) !== false) {
foreach ((array) $arg as $key => $val) {
if (is_array($val) && isset($result[$key]) && is_array($result[$key])) {
$result[$key] = static::merge($result[$key], $val);
} elseif (is_int($key)) {
$result[] = $val;
} else {
$result[$key] = $val;
}
}
}
return $result;
}