1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Returns a chunked array with calculated chunk size. |
5
|
|
|
* |
6
|
|
|
* @param array $array |
7
|
|
|
* @param int $min |
8
|
|
|
* @param int|null $max |
9
|
|
|
* @param bool $no_remainders |
10
|
|
|
* @param bool $preserve_keys |
11
|
|
|
* @return array |
12
|
|
|
*/ |
13
|
|
|
function arrayChunks(array $array, $min = 0, $max = null, $no_remainders = false, $preserve_keys = true) |
14
|
|
|
{ |
15
|
|
|
$chunks = array_chunk($array, chunkSizer(count($array), $min, $max), $preserve_keys); |
|
|
|
|
16
|
|
|
|
17
|
|
|
// Check if the first chunk is the same length as the last chunk |
18
|
|
|
if ($no_remainders && count($chunks[0]) != count(array_reverse($chunks)[0])) { |
19
|
|
|
$remainder = array_pop($chunks); |
20
|
|
|
$last_chunk = array_pop($chunks); |
21
|
|
|
|
22
|
|
|
// Add the remainder chunk to the last equal sized chunk |
23
|
|
|
$chunks[] = array_merge($last_chunk, $remainder); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $chunks; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Flatten a multidimensional array into a 2D array without nested keys. |
31
|
|
|
* |
32
|
|
|
* @param $array |
33
|
|
|
* @param bool $nest_keys |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
function arrayFlattenKeys($array, $nest_keys = true) |
37
|
|
|
{ |
38
|
|
|
$flat = []; |
39
|
|
|
foreach (array_keys($array) as $key) { |
40
|
|
|
if (is_array($array[$key])) { |
41
|
|
|
// If the key is an array, add each children keys to flattened array |
42
|
|
|
foreach ($array[$key] as $k => $v) { |
43
|
|
|
if ($nest_keys) { |
44
|
|
|
$flat[$key.'_'.$k] = $v; |
45
|
|
|
} else { |
46
|
|
|
$flat[$k] = $v; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
$flat[$key] = $array[$key]; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $flat; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Remove particular keys from a multidimensional array. |
59
|
|
|
* |
60
|
|
|
* @param $array |
61
|
|
|
* @param $keys |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
function arrayRemoveKeys($array, $keys) |
65
|
|
|
{ |
66
|
|
|
$all_keys = array_keys($array); |
67
|
|
|
foreach ((array) $keys as $key) { |
68
|
|
|
if (in_array($key, $all_keys)) { |
69
|
|
|
unset($array[$key]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $array; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sum the values of two arrays |
78
|
|
|
* |
79
|
|
|
* @param $array1 |
80
|
|
|
* @param $array2 |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
function sumArrays($array1, $array2) |
84
|
|
|
{ |
85
|
|
|
$array = []; |
86
|
|
|
foreach ($array1 as $index => $value) { |
87
|
|
|
$array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $array; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Determine if all values in an array of key => value pairs are unique. |
95
|
|
|
* |
96
|
|
|
* @param array $array |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
function arrayValuesUnique(array $array) |
100
|
|
|
{ |
101
|
|
|
// Count the number of unique array values |
102
|
|
|
// Check to see if there is more than unique array_value |
103
|
|
|
return count(array_unique(array_values($array))) > 1; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Determine if all array_values are equal to a certain value. |
108
|
|
|
* |
109
|
|
|
* @param array $array |
110
|
|
|
* @param mixed $value |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
function arrayValuesEqual(array $array, $value) |
114
|
|
|
{ |
115
|
|
|
// Check if all array values are equal to a certain value |
116
|
|
|
return count(array_keys($array, $value)) == count($array); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Determine if an array is multidimensional and has keys. |
121
|
|
|
* |
122
|
|
|
* @param array $array |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
function arrayHasKeys(array $array) |
126
|
|
|
{ |
127
|
|
|
return count($array) == count($array, COUNT_RECURSIVE); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Map an array with associated keys. |
132
|
|
|
* |
133
|
|
|
* @param callable $f |
134
|
|
|
* @param array $a |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
function array_map_assoc(callable $f, $a) |
138
|
|
|
{ |
139
|
|
|
return array_column(array_map($f, array_keys($a), $a), 1, 0); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (! function_exists('array_except')) { |
143
|
|
|
/** |
144
|
|
|
* Remove specific arrays of keys without modifying the original array. |
145
|
|
|
* |
146
|
|
|
* @param array $original |
147
|
|
|
* @param array $except |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
function array_except(array $original, array $except) |
151
|
|
|
{ |
152
|
|
|
return array_diff_key($original, array_flip((array) $except)); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return a best fit chunk size to be passed to array_chunks functions. |
158
|
|
|
* |
159
|
|
|
* Calculates the remainder of array sizes divided by the divisor |
160
|
|
|
* using modulus division. Continues to calculate remainders until |
161
|
|
|
* the remainder is zero, signifying evenly sized chunks, or the |
162
|
|
|
* divisor is equal to the array size. If a remainder of zero is not |
163
|
|
|
* found the lowest remainder is returned. |
164
|
|
|
* |
165
|
|
|
* @param int $array_size |
166
|
|
|
* @param int $min minimum chunk size |
167
|
|
|
* @param null $max maximum chunk size |
|
|
|
|
168
|
|
|
* @param int $divisor |
169
|
|
|
* @return int $remainder lowest calculated remainder |
170
|
|
|
*/ |
171
|
|
|
function chunkSizer($array_size, $min = 0, $max = null, $divisor = 2) |
172
|
|
|
{ |
173
|
|
|
// If the size of the array is a perfect square, return the square root |
174
|
|
|
if (gmp_perfect_square($array_size) == true) { |
|
|
|
|
175
|
|
|
return sqrt($array_size); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// If min and max are the same return that value |
179
|
|
|
elseif ($min == $max) { |
180
|
|
|
return $min; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$max = (isset($max) ? $max : $array_size); |
184
|
|
|
$sizes = []; |
185
|
|
|
while ($divisor < $max) { |
186
|
|
|
$sizes[$divisor] = [ |
187
|
|
|
// Number of chunks |
188
|
|
|
'rows'=> floor($array_size / $divisor), |
189
|
|
|
|
190
|
|
|
// Items in each chunk |
191
|
|
|
'cols' => $divisor, |
192
|
|
|
|
193
|
|
|
// Left over items in last chunk |
194
|
|
|
'remainder' => $array_size % $divisor, |
195
|
|
|
]; |
196
|
|
|
$divisor++; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Filter sizes by column values |
200
|
|
|
return min(array_filter(array_column($sizes, 'cols', 'cols'), function ($size) use ($min, $max, $sizes) { |
201
|
|
|
return |
202
|
|
|
// Check that the remainder is no more than half of the number of columns |
203
|
|
|
($sizes[$size]['remainder'] == 0 || $sizes[$size]['remainder'] >= $size / 2) && |
204
|
|
|
|
205
|
|
|
// Check that the number of columns is greater than or equal than min and less than or equal than max |
206
|
|
|
$min <= $size && $size <= $max; |
207
|
|
|
} |
208
|
|
|
)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Only declare function if Illuminate/Collection is installed. |
213
|
|
|
* |
214
|
|
|
* todo: add composer package suggestion |
215
|
|
|
*/ |
216
|
|
|
if (function_exists('collect')) { |
217
|
|
|
/** |
218
|
|
|
* Return a flat array of values found in the $first array that are not found in the $second. |
219
|
|
|
* |
220
|
|
|
* @param array $first |
221
|
|
|
* @param array $second |
222
|
|
|
* @param bool $toArray |
223
|
|
|
* @return Illuminate\Support\Collection|array |
224
|
|
|
*/ |
225
|
|
|
function array_diff_flat(array $first, array $second, bool $toArray = true) |
226
|
|
|
{ |
227
|
|
|
$collection = collect($first) |
228
|
|
|
->diff($second) |
229
|
|
|
->flatten(); |
230
|
|
|
|
231
|
|
|
// Return as array |
232
|
|
|
if ($toArray) { |
233
|
|
|
return $collection->toArray(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Return as Collection |
237
|
|
|
return $collection; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Remove a key from an array & return the key's value. |
243
|
|
|
* |
244
|
|
|
* @param array $array |
245
|
|
|
* @param string $key |
246
|
|
|
* @return mixed |
247
|
|
|
*/ |
248
|
|
|
function arrayUnset(array $array, $key) |
249
|
|
|
{ |
250
|
|
|
// Get the value |
251
|
|
|
try { |
252
|
|
|
$value = $array[$key]; |
253
|
|
|
} catch (ErrorException $exception) { |
254
|
|
|
$value = null; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// Remove the value from the array |
258
|
|
|
unset($array[$key]); |
259
|
|
|
|
260
|
|
|
// Return the value |
261
|
|
|
return $value; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Determine if all values in an array are null. |
266
|
|
|
* |
267
|
|
|
* @param array $array |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
|
|
function arrayValuesNull(array $array) |
271
|
|
|
{ |
272
|
|
|
return arrayValuesEqual($array, null); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @deprecated |
277
|
|
|
* |
278
|
|
|
* Sum the values of two arrays |
279
|
|
|
* |
280
|
|
|
* @param $array1 |
281
|
|
|
* @param $array2 |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
|
|
function sum_arrays($array1, $array2) |
285
|
|
|
{ |
286
|
|
|
return sumArrays($array1, $array2); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @deprecated |
291
|
|
|
* |
292
|
|
|
* Remove a key from an array & return the key's value. |
293
|
|
|
* |
294
|
|
|
* @param array $array |
295
|
|
|
* @param string $key |
296
|
|
|
* @return mixed |
297
|
|
|
*/ |
298
|
|
|
function array_unset(array $array, $key) |
299
|
|
|
{ |
300
|
|
|
return arrayUnset($array, $key); |
301
|
|
|
} |
302
|
|
|
|