2 namespace Consolidation\Config\Util;
5 * Useful array utilities.
10 * Merges arrays recursively while preserving.
12 * @param array $array1
13 * @param array $array2
17 * @see http://php.net/manual/en/function.array-merge-recursive.php#92195
18 * @see https://github.com/grasmash/bolt/blob/robo-rebase/src/Robo/Common/ArrayManipulator.php#L22
20 public static function mergeRecursiveDistinct(
25 foreach ($array2 as $key => &$value) {
26 $merged[$key] = self::mergeRecursiveValue($merged, $key, $value);
32 * Process the value in an mergeRecursiveDistinct - make a recursive
35 protected static function mergeRecursiveValue(&$merged, $key, $value)
37 if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
38 return self::mergeRecursiveDistinct($merged[$key], $value);
45 * Merges arrays recursively while preserving.
47 * @param array $array1
48 * @param array $array2
52 * @see http://php.net/manual/en/function.array-merge-recursive.php#92195
53 * @see https://github.com/grasmash/bolt/blob/robo-rebase/src/Robo/Common/ArrayManipulator.php#L22
55 public static function mergeRecursiveSelect(
62 foreach ($array2 as $key => &$value) {
63 $merged[$key] = self::mergeRecursiveSelectValue($merged, $key, $value, $selectionList, $keyPrefix);
69 * Process the value in an mergeRecursiveDistinct - make a recursive
72 protected static function mergeRecursiveSelectValue(&$merged, $key, $value, $selectionList, $keyPrefix)
74 if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
75 if (self::selectMerge($keyPrefix, $key, $selectionList)) {
76 return array_merge_recursive($merged[$key], $value);
78 return self::mergeRecursiveSelect($merged[$key], $value, $selectionList, "${keyPrefix}${key}.");
84 protected static function selectMerge($keyPrefix, $key, $selectionList)
86 return in_array("${keyPrefix}${key}", $selectionList);
91 * Fills all of the leaf-node values of a nested array with the
92 * provided replacement value.
94 public static function fillRecursive(array $data, $fill)
97 foreach ($data as $key => $value) {
98 $result[$key] = $fill;
99 if (self::isAssociative($value)) {
100 $result[$key] = self::fillRecursive($value, $fill);
107 * Return true if the provided parameter is an array, and at least
108 * one key is non-numeric.
110 public static function isAssociative($testArray)
112 if (!is_array($testArray)) {
115 foreach (array_keys($testArray) as $key) {
116 if (!is_numeric($key)) {