|
9 | 9 | * the LICENSE file that was distributed with this source code. |
10 | 10 | */ |
11 | 11 |
|
| 12 | +use CodeIgniter\Helpers\Array\ArrayHelper; |
| 13 | + |
12 | 14 | // CodeIgniter Array Helpers |
13 | 15 |
|
14 | 16 | if (! function_exists('dot_array_search')) { |
|
20 | 22 | */ |
21 | 23 | function dot_array_search(string $index, array $array) |
22 | 24 | { |
23 | | - // See https://regex101.com/r/44Ipql/1 |
24 | | - $segments = preg_split( |
25 | | - '/(?<!\\\\)\./', |
26 | | - rtrim($index, '* '), |
27 | | - 0, |
28 | | - PREG_SPLIT_NO_EMPTY |
29 | | - ); |
30 | | - |
31 | | - $segments = array_map(static fn ($key) => str_replace('\.', '.', $key), $segments); |
32 | | - |
33 | | - return _array_search_dot($segments, $array); |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -if (! function_exists('_array_search_dot')) { |
38 | | - /** |
39 | | - * Used by `dot_array_search` to recursively search the |
40 | | - * array with wildcards. |
41 | | - * |
42 | | - * @internal This should not be used on its own. |
43 | | - * |
44 | | - * @return array|bool|float|int|object|string|null |
45 | | - */ |
46 | | - function _array_search_dot(array $indexes, array $array) |
47 | | - { |
48 | | - // If index is empty, returns null. |
49 | | - if ($indexes === []) { |
50 | | - return null; |
51 | | - } |
52 | | - |
53 | | - // Grab the current index |
54 | | - $currentIndex = array_shift($indexes); |
55 | | - |
56 | | - if (! isset($array[$currentIndex]) && $currentIndex !== '*') { |
57 | | - return null; |
58 | | - } |
59 | | - |
60 | | - // Handle Wildcard (*) |
61 | | - if ($currentIndex === '*') { |
62 | | - $answer = []; |
63 | | - |
64 | | - foreach ($array as $value) { |
65 | | - if (! is_array($value)) { |
66 | | - return null; |
67 | | - } |
68 | | - |
69 | | - $answer[] = _array_search_dot($indexes, $value); |
70 | | - } |
71 | | - |
72 | | - $answer = array_filter($answer, static fn ($value) => $value !== null); |
73 | | - |
74 | | - if ($answer !== []) { |
75 | | - if (count($answer) === 1) { |
76 | | - // If array only has one element, we return that element for BC. |
77 | | - return current($answer); |
78 | | - } |
79 | | - |
80 | | - return $answer; |
81 | | - } |
82 | | - |
83 | | - return null; |
84 | | - } |
85 | | - |
86 | | - // If this is the last index, make sure to return it now, |
87 | | - // and not try to recurse through things. |
88 | | - if (empty($indexes)) { |
89 | | - return $array[$currentIndex]; |
90 | | - } |
91 | | - |
92 | | - // Do we need to recursively search this value? |
93 | | - if (is_array($array[$currentIndex]) && $array[$currentIndex] !== []) { |
94 | | - return _array_search_dot($indexes, $array[$currentIndex]); |
95 | | - } |
96 | | - |
97 | | - // Otherwise, not found. |
98 | | - return null; |
| 25 | + return ArrayHelper::dotArraySearch($index, $array); |
99 | 26 | } |
100 | 27 | } |
101 | 28 |
|
@@ -231,55 +158,6 @@ function array_flatten_with_dots(iterable $array, string $id = ''): array |
231 | 158 | */ |
232 | 159 | function array_group_by(array $array, array $indexes, bool $includeEmpty = false): array |
233 | 160 | { |
234 | | - if ($indexes === []) { |
235 | | - return $array; |
236 | | - } |
237 | | - |
238 | | - $result = []; |
239 | | - |
240 | | - foreach ($array as $row) { |
241 | | - $result = _array_attach_indexed_value($result, $row, $indexes, $includeEmpty); |
242 | | - } |
243 | | - |
244 | | - return $result; |
245 | | - } |
246 | | -} |
247 | | - |
248 | | -if (! function_exists('_array_attach_indexed_value')) { |
249 | | - /** |
250 | | - * Used by `array_group_by` to recursively attach $row to the $indexes path of values found by |
251 | | - * `dot_array_search` |
252 | | - * |
253 | | - * @internal This should not be used on its own |
254 | | - */ |
255 | | - function _array_attach_indexed_value(array $result, array $row, array $indexes, bool $includeEmpty): array |
256 | | - { |
257 | | - if (($index = array_shift($indexes)) === null) { |
258 | | - $result[] = $row; |
259 | | - |
260 | | - return $result; |
261 | | - } |
262 | | - |
263 | | - $value = dot_array_search($index, $row); |
264 | | - |
265 | | - if (! is_scalar($value)) { |
266 | | - $value = ''; |
267 | | - } |
268 | | - |
269 | | - if (is_bool($value)) { |
270 | | - $value = (int) $value; |
271 | | - } |
272 | | - |
273 | | - if (! $includeEmpty && $value === '') { |
274 | | - return $result; |
275 | | - } |
276 | | - |
277 | | - if (! array_key_exists($value, $result)) { |
278 | | - $result[$value] = []; |
279 | | - } |
280 | | - |
281 | | - $result[$value] = _array_attach_indexed_value($result[$value], $row, $indexes, $includeEmpty); |
282 | | - |
283 | | - return $result; |
| 161 | + return ArrayHelper::arrayGroupBy($array, $indexes, $includeEmpty); |
284 | 162 | } |
285 | 163 | } |
0 commit comments