forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.php
44 lines (42 loc) · 1.14 KB
/
merge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace collections;
/**
* Recursively combines and merge collections provided with each others.
*
* - If the collections have common keys, then the last passed keys override the previous.
* - If numerical indexes are passed, then last passed indexes override the previous.
*
* For a non-recursive merge, see `__::merge()`.
*
* **Usage**
*
* ```php
* __::merge(
* ['color' => ['favorite' => 'red', 'model' => 3, 5], 3],
* [10, 'color' => ['favorite' => 'green', 'blue']]
* );
* ```
*
* **Result**
*
* ```
* ['color' => ['favorite' => 'green', 'model' => 3, 'blue'], 10]
* ```
*
* @param array|object ...$_ Collections to merge.
*
* @return array|object Concatenated collection.
*/
function merge()
{
return \__::reduceRight(func_get_args(), function ($source, $result) {
\__::doForEach($source, function ($sourceValue, $key) use (&$result) {
$value = $sourceValue;
if (\__::isCollection($value)) {
$value = merge(\__::get($result, $key), $sourceValue);
}
$result = \__::set($result, $key, $value);
});
return $result;
}, []);
}