|
2 | 2 |
|
3 | 3 | namespace Loilo\JsonPath; |
4 | 4 |
|
5 | | -/** |
6 | | - * Map a callback over the elements of one or more arrays and flatten the result |
7 | | - */ |
8 | | -function array_flat_map(callable $callback, ...$arrays): array |
9 | | -{ |
10 | | - if (sizeof($arrays) === 0) { |
11 | | - return []; |
12 | | - } |
13 | | - |
14 | | - $new_array = []; |
15 | | - $maxlength = max(array_map('sizeof', $arrays)); |
16 | | - for ($i = 0; $i < $maxlength; $i++) { |
17 | | - $args = |
18 | | - sizeof($arrays) === 1 |
19 | | - ? [$arrays[0][$i]] |
20 | | - : array_map(fn($array) => $array[$i] ?? null, $arrays); |
21 | | - $result = call_user_func_array($callback, $args); |
22 | | - |
23 | | - if (is_array($result)) { |
24 | | - if (empty($result)) { |
25 | | - continue; |
26 | | - } |
27 | | - |
28 | | - $new_array = array_merge($new_array, $result); |
29 | | - } else { |
30 | | - $new_array[] = $result; |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - return $new_array; |
35 | | -} |
36 | | - |
37 | | -/** |
38 | | - * Flatten a multi-dimensional array by a given number of levels |
39 | | - */ |
40 | | -function array_flatten(array $array, float $levels = 1): array |
41 | | -{ |
42 | | - if ($levels <= 0) { |
43 | | - return $array; |
44 | | - } |
45 | | - |
46 | | - $result = []; |
47 | | - foreach ($array as $key => $value) { |
48 | | - if (is_array($value)) { |
49 | | - $result = array_merge($result, array_flatten($value, $levels - 1)); |
50 | | - } else { |
51 | | - $result = array_merge($result, [$key => $value]); |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - return $result; |
56 | | -} |
57 | | - |
58 | | -/** |
59 | | - * Filter an array and re-indexes it |
60 | | - */ |
61 | | -function array_filter_2(array $array, ?callable $callback = null): array |
62 | | -{ |
63 | | - return array_values(array_filter($array, $callback)); |
64 | | -} |
65 | | - |
66 | | -/** |
67 | | - * Check if an array is a list |
68 | | - */ |
69 | | -function array_is_list_2(array $array): bool |
70 | | -{ |
71 | | - if (function_exists('array_is_list')) { |
72 | | - return \array_is_list($array); |
73 | | - } |
74 | | - |
75 | | - return array_keys($array) === range(0, count($array) - 1); |
76 | | -} |
77 | | - |
78 | 5 | /** |
79 | 6 | * Check if a value is a number |
80 | 7 | */ |
81 | 8 | function is_number($value): bool |
82 | 9 | { |
83 | 10 | return is_int($value) || is_float($value); |
84 | 11 | } |
85 | | - |
86 | | -/** |
87 | | - * Normalize an index for a given array length to be positive |
88 | | - * or null if it is out of bounds |
89 | | - * An index is out of bounds if it is less than the negative length |
90 | | - * or greater than or equal to the length |
91 | | - */ |
92 | | -function normalize_index($index, int $length): ?int |
93 | | -{ |
94 | | - if ($index < -$length || $index >= $length) { |
95 | | - return null; |
96 | | - } |
97 | | - |
98 | | - if ($index < 0) { |
99 | | - $index += $length; |
100 | | - } |
101 | | - |
102 | | - return $index; |
103 | | -} |
0 commit comments