mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-09 01:27:20 +08:00
...
This commit is contained in:
@ -19,7 +19,7 @@ namespace yii\util;
|
|||||||
class ArrayHelper extends \yii\base\Component
|
class ArrayHelper extends \yii\base\Component
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Merges two arrays into one recursively.
|
* Merges two or more arrays into one recursively.
|
||||||
* If each array has an element with the same string key value, the latter
|
* If each array has an element with the same string key value, the latter
|
||||||
* will overwrite the former (different from array_merge_recursive).
|
* will overwrite the former (different from array_merge_recursive).
|
||||||
* Recursive merging will be conducted if both arrays have an element of array
|
* Recursive merging will be conducted if both arrays have an element of array
|
||||||
@ -27,22 +27,27 @@ class ArrayHelper extends \yii\base\Component
|
|||||||
* For integer-keyed elements, the elements from the latter array will
|
* For integer-keyed elements, the elements from the latter array will
|
||||||
* be appended to the former array.
|
* be appended to the former array.
|
||||||
* @param array $a array to be merged to
|
* @param array $a array to be merged to
|
||||||
* @param array $b array to be merged from
|
* @param array $b array to be merged from. You can specify additional
|
||||||
|
* arrays via third argument, fourth argument etc.
|
||||||
* @return array the merged array (the original arrays are not changed.)
|
* @return array the merged array (the original arrays are not changed.)
|
||||||
* @see mergeWith
|
|
||||||
*/
|
*/
|
||||||
public static function merge($a, $b)
|
public static function merge($a, $b)
|
||||||
{
|
{
|
||||||
foreach ($b as $k => $v) {
|
$args = func_get_args();
|
||||||
if (is_integer($k)) {
|
$res = array_shift($args);
|
||||||
isset($a[$k]) ? $a[] = $v : $a[$k] = $v;
|
while ($args !== array()) {
|
||||||
} elseif (is_array($v) && isset($a[$k]) && is_array($a[$k])) {
|
$next = array_shift($args);
|
||||||
$a[$k] = static::merge($a[$k], $v);
|
foreach ($next as $k => $v) {
|
||||||
} else {
|
if (is_integer($k)) {
|
||||||
$a[$k] = $v;
|
isset($res[$k]) ? $res[] = $v : $res[$k] = $v;
|
||||||
|
} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
|
||||||
|
$res[$k] = self::merge($res[$k], $v);
|
||||||
|
} else {
|
||||||
|
$res[$k] = $v;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $a;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user