1 | <?php |
||
5 | class Arr |
||
6 | { |
||
7 | /** |
||
8 | * Get a random value from an array. |
||
9 | * |
||
10 | * @param array $array |
||
11 | * @param int $numReq The amount of values to return |
||
12 | * |
||
13 | * @return mixed |
||
14 | */ |
||
15 | public static function randValue(array $array, $numReq = 1) |
||
29 | |||
30 | /** |
||
31 | * Get a random value from an array, with the ability to skew the results. |
||
32 | * Example: Arr::randWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
||
33 | * |
||
34 | * @param array $array |
||
35 | * |
||
36 | * @return mixed |
||
37 | */ |
||
38 | public static function randWeighted(array $array) |
||
50 | |||
51 | /** |
||
52 | * Determine if all given needles are present in the haystack. |
||
53 | * |
||
54 | * @param array|string $needles |
||
55 | * @param array $haystack |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | public static function valuesInArray($needles, array $haystack) |
||
67 | |||
68 | /** |
||
69 | * Determine if all given needles are present in the haystack as array keys. |
||
70 | * |
||
71 | * @param array|string $needles |
||
72 | * @param array $haystack |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public static function keysExist($needles, array $haystack) |
||
84 | |||
85 | /** |
||
86 | * Returns an array with two elements. |
||
87 | * |
||
88 | * Iterates over each value in the array passing them to the callback function. |
||
89 | * If the callback function returns true, the current value from array is returned in the first |
||
90 | * element of result array. If not, it is return in the second element of result array. |
||
91 | * |
||
92 | * Array keys are preserved. |
||
93 | * |
||
94 | * @param array $array |
||
95 | * @param callable $callback |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public static function splitFilter(array $array, callable $callback) |
||
109 | |||
110 | /** |
||
111 | * Split an array in the given amount of pieces. |
||
112 | * |
||
113 | * @param array $array |
||
114 | * @param int $numberOfPieces |
||
115 | * @param bool $preserveKeys |
||
116 | * @throws \InvalidArgumentException if the provided argument $numberOfPieces is lower than 1 |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public static function split(array $array, $numberOfPieces = 2, $preserveKeys = false) |
||
134 | |||
135 | /** |
||
136 | * Returns an array with the unique values from all the given arrays. |
||
137 | * |
||
138 | * @param \array[] $arrays |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public static function mergeValues(array...$arrays) |
||
150 | |||
151 | /** |
||
152 | * Flatten an array of arrays. The `$levels` parameter specifies how deep you want to |
||
153 | * recurse in the array. If `$levels` is -1, the function will recurse infinitely. |
||
154 | * |
||
155 | * @param array $array |
||
156 | * @param int $levels |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | public static function flatten(array $array, $levels = -1) |
||
178 | } |
||
179 |