1 | <?php |
||
7 | class Arr |
||
8 | { |
||
9 | /** |
||
10 | * Flatten a multi-dimensional array into a single level |
||
11 | * |
||
12 | * @param array $array |
||
13 | * @param int $depth |
||
14 | * @return array |
||
15 | */ |
||
16 | public static function flatten(array $array, $depth = INF) : array |
||
17 | { |
||
18 | $depth = is_int($depth) ? max($depth, 0) : INF; |
||
19 | |||
20 | return array_reduce($array, function ($result, $item) use ($depth) { |
||
21 | if (!is_array($item) || $depth === 0) { |
||
22 | return array_merge($result, [$item]); |
||
23 | } elseif ($depth === 1) { |
||
24 | return array_merge($result, array_values($item)); |
||
25 | } else { |
||
26 | return array_merge($result, self::flatten($item, $depth - 1)); |
||
27 | } |
||
28 | }, []); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Flatten a multi-dimensional array into a single level with saving keys |
||
33 | * |
||
34 | * @param array $array |
||
35 | * @param int $depth |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | public static function flattenAssoc(array $array, $depth = INF) : array |
||
54 | |||
55 | /** |
||
56 | * Get depth of array |
||
57 | * |
||
58 | * @param array $array |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | public static function depth(array $array) : int |
||
78 | |||
79 | /** |
||
80 | * Check if array contains only a specific type |
||
81 | * |
||
82 | * @param array $array |
||
83 | * @param string $type |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | public static function isTypeOf(array $array, string $type) : bool |
||
103 | |||
104 | /** |
||
105 | * Check if array contains only a specific type |
||
106 | * |
||
107 | * @param array $array |
||
108 | * @param array $types |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public static function isTypesOf(array $array, array $types) : bool |
||
134 | |||
135 | /** |
||
136 | * Check if array contains instances of specified class/interface |
||
137 | * |
||
138 | * @param array $array |
||
139 | * @param string $className |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public static function isInstancesOf(array $array, string $className) : bool |
||
157 | } |
||
158 |