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