|
1
|
|
|
<?php namespace Arcanedev\Composer\Utilities; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class NestedArray |
|
5
|
|
|
* |
|
6
|
|
|
* @package Arcanedev\Composer\Utilities |
|
7
|
|
|
* @author ARCANEDEV <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
class NestedArray |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Merges multiple arrays, recursively, and returns the merged array. |
|
14
|
|
|
* |
|
15
|
|
|
* This function is similar to PHP's array_merge_recursive() function, but it handles non-array values differently. |
|
16
|
|
|
* When merging values that are not both arrays, the latter value replaces the former rather than merging with it. |
|
17
|
|
|
* |
|
18
|
|
|
* |
|
19
|
|
|
* @return array |
|
20
|
|
|
* |
|
21
|
|
|
* @see NestedArray::mergeDeepArray() |
|
22
|
|
|
*/ |
|
23
|
15 |
|
public static function mergeDeep() |
|
24
|
|
|
{ |
|
25
|
15 |
|
return self::mergeDeepArray(func_get_args()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Merges multiple arrays, recursively, and returns the merged array. |
|
30
|
|
|
* |
|
31
|
|
|
* This function is equivalent to NestedArray::mergeDeep(), except the input arrays are passed as a single array |
|
32
|
|
|
* parameter rather than a variable parameter list. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $arrays |
|
35
|
|
|
* @param bool $preserveIntegerKeys |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
* |
|
39
|
|
|
* @see NestedArray::mergeDeep() |
|
40
|
|
|
*/ |
|
41
|
30 |
|
public static function mergeDeepArray(array $arrays, $preserveIntegerKeys = false) |
|
42
|
|
|
{ |
|
43
|
30 |
|
$result = []; |
|
44
|
|
|
|
|
45
|
30 |
|
foreach ($arrays as $array) { |
|
46
|
30 |
|
foreach ($array as $key => $value) { |
|
47
|
|
|
// Renumber integer keys as array_merge_recursive() does unless $preserve_integer_keys is set to TRUE. |
|
48
|
|
|
// Note that PHP automatically converts array keys that are integer strings (e.g., '1') to integers. |
|
49
|
30 |
|
if (is_integer($key) && ! $preserveIntegerKeys) { |
|
50
|
30 |
|
$result[] = $value; |
|
51
|
24 |
|
} |
|
52
|
30 |
|
elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) { |
|
53
|
|
|
// Recurse when both values are arrays. |
|
54
|
30 |
|
$result[$key] = self::mergeDeepArray(array($result[$key], $value), $preserveIntegerKeys); |
|
55
|
24 |
|
} |
|
56
|
|
|
else { |
|
57
|
|
|
// Otherwise, use the latter value, overriding any previous value. |
|
58
|
30 |
|
$result[$key] = $value; |
|
59
|
|
|
} |
|
60
|
24 |
|
} |
|
61
|
24 |
|
} |
|
62
|
|
|
|
|
63
|
30 |
|
return $result; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|