1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Get a random value from an array. |
7
|
|
|
* |
8
|
|
|
* @param array $array |
9
|
|
|
* @param int $numReq The amount of values to return |
10
|
|
|
* |
11
|
|
|
* @return mixed |
12
|
|
|
*/ |
13
|
|
|
function array_rand_value(array $array, $numReq = 1) |
14
|
|
|
{ |
15
|
|
|
if (!count($array)) { |
16
|
|
|
return; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
$keys = array_rand($array, $numReq); |
20
|
|
|
|
21
|
|
|
if ($numReq === 1) { |
22
|
|
|
return $array[$keys]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return array_intersect_key($array, array_flip($keys)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get a random value from an array, with the ability to skew the results. |
30
|
|
|
* Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
31
|
|
|
* |
32
|
|
|
* @param array $array |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
function array_rand_weighted(array $array) |
37
|
|
|
{ |
38
|
|
|
$options = []; |
39
|
|
|
|
40
|
|
|
foreach ($array as $option => $weight) { |
41
|
|
|
for ($i = 0; $i < $weight; ++$i) { |
42
|
|
|
$options[] = $option; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return array_rand_value($options); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Determine if all given needles are present in the haystack. |
51
|
|
|
* |
52
|
|
|
* @param array|string $needles |
53
|
|
|
* @param array $haystack |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
function values_in_array($needles, array $haystack) |
58
|
|
|
{ |
59
|
|
|
if (!is_array($needles)) { |
60
|
|
|
$needles = [$needles]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return count(array_intersect($needles, $haystack)) === count($needles); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Determine if all given needles are present in the haystack as array keys. |
68
|
|
|
* |
69
|
|
|
* @param array|string $needles |
70
|
|
|
* @param array $haystack |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
function array_keys_exist($needles, array $haystack) |
75
|
|
|
{ |
76
|
|
|
if (!is_array($needles)) { |
77
|
|
|
return array_key_exists($needles, $haystack); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return values_in_array($needles, array_keys($haystack)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns an array with two elements. |
85
|
|
|
* |
86
|
|
|
* Iterates over each value in the array passing them to the callback function. |
87
|
|
|
* If the callback function returns true, the current value from array is returned in the first |
88
|
|
|
* element of result array. If not, it is return in the second element of result array. |
89
|
|
|
* |
90
|
|
|
* Array keys are preserved. |
91
|
|
|
* |
92
|
|
|
* @param array $array |
93
|
|
|
* @param callable $callback |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
function array_split_filter(array $array, callable $callback) |
98
|
|
|
{ |
99
|
|
|
$passesFilter = array_filter($array, $callback); |
100
|
|
|
|
101
|
|
|
$negatedCallback = function ($item) use ($callback) { |
102
|
|
|
return !$callback($item); |
103
|
|
|
}; |
104
|
|
|
|
105
|
|
|
$doesNotPassFilter = array_filter($array, $negatedCallback); |
106
|
|
|
|
107
|
|
|
return [$passesFilter, $doesNotPassFilter]; |
108
|
|
|
} |
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
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false) |
120
|
|
|
{ |
121
|
|
|
if (count($array) === 0) { |
122
|
|
|
return []; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$splitSize = ceil(count($array) / $numberOfPieces); |
126
|
|
|
|
127
|
|
|
return array_chunk($array, $splitSize, $preserveKeys); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns an array with the unique values from all the given arrays. |
132
|
|
|
* |
133
|
|
|
* @param \array[] $arrays |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
function array_merge_values(array ...$arrays) |
138
|
|
|
{ |
139
|
|
|
$allValues = array_reduce($arrays, function ($carry, $array) { |
140
|
|
|
return array_merge($carry, $array); |
141
|
|
|
}, []); |
142
|
|
|
|
143
|
|
|
return array_values(array_unique($allValues)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Flatten an array of arrays. The `$levels` parameter specifies how deep you want to |
148
|
|
|
* recurse in the array. If `$levels` is -1, the function will recurse infinitely. |
149
|
|
|
* |
150
|
|
|
* @param array $array |
151
|
|
|
* @param int $levels |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
function array_flatten(array $array, $levels = -1) |
156
|
|
|
{ |
157
|
|
|
if ($levels === 0) { |
158
|
|
|
return $array; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$flattened = []; |
162
|
|
|
|
163
|
|
|
if ($levels !== -1) { |
164
|
|
|
--$levels; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
foreach ($array as $element) { |
168
|
|
|
$flattened = array_merge( |
169
|
|
|
$flattened, |
170
|
|
|
is_array($element) ? array_flatten($element, $levels) : [$element] |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $flattened; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* recurse array key from snake_case to camelCase |
179
|
|
|
* @param array $array |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
function array_keys_snake2camel(array $array) |
183
|
|
|
{ |
184
|
|
|
foreach ($array as $key => &$sub) { |
185
|
|
|
if (strpos($key, '_') !== false) { |
186
|
|
|
$newKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))); |
187
|
|
|
$array[$newKey] = $sub; |
188
|
|
|
unset ($array[$key]); |
189
|
|
|
} elseif (is_array($sub)) { |
190
|
|
|
$sub = array_keys_snake2camel($sub); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $array; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* recurse array key from camelCase to snake_case |
199
|
|
|
* @param array $array |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
function array_keys_camel2snake(array $array) |
203
|
|
|
{ |
204
|
|
|
foreach ($array as $key => &$sub) { |
205
|
|
|
if (preg_match('/(?<!^)[A-Z]/', $key)) { |
206
|
|
|
$newKey = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key)); |
207
|
|
|
$array[$newKey] = $sub; |
208
|
|
|
unset($array[$key]); |
209
|
|
|
} else if (is_array($sub)) { |
210
|
|
|
$sub = array_keys_camel2snake($sub); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
return $array; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|