1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!function_exists('get')) { |
4
|
|
|
/** |
5
|
|
|
* Get an item from an array using "dot" notation. |
6
|
|
|
* |
7
|
|
|
* @param array $array |
8
|
|
|
* @param string $key |
9
|
|
|
* @param mixed $default |
10
|
|
|
* @return mixed |
11
|
|
|
*/ |
12
|
|
|
function get($array, $key, $default = null) |
13
|
|
|
{ |
14
|
|
|
if (is_null($key)) { |
15
|
|
|
return $array; |
16
|
|
|
} |
17
|
|
|
if (isset($array[$key])) { |
18
|
|
|
return $array[$key]; |
19
|
|
|
} |
20
|
|
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
21
|
|
|
if (!array_key_exists_safe($segment, $array)) { |
22
|
|
|
return value($default); |
23
|
|
|
} |
24
|
|
|
$array = $array[$segment]; |
25
|
|
|
} |
26
|
|
|
return $array; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!function_exists('head')) { |
31
|
|
|
/** |
32
|
|
|
* Get the first element of an array. Useful for method chaining. |
33
|
|
|
* |
34
|
|
|
* @param array $array |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
function head($array) |
38
|
|
|
{ |
39
|
|
|
return reset($array); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!function_exists('last')) { |
44
|
|
|
/** |
45
|
|
|
* Get the last element from an array. |
46
|
|
|
* |
47
|
|
|
* @param array $array |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
function last($array) |
51
|
|
|
{ |
52
|
|
|
return end($array); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!function_exists('insert_at_top')) { |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Insert element in top of array and return $count element. |
60
|
|
|
* @param $my_arr |
61
|
|
|
* @param $elem |
62
|
|
|
* @param int $count |
63
|
|
|
* @return bool |
64
|
|
|
* @see https://github.com/ifsnop/lpsf/blob/master/src/Ifsnop/functions.inc.php |
65
|
|
|
*/ |
66
|
|
|
function insert_at_top(&$my_arr, $elem, int $count = 10): bool |
67
|
|
|
{ |
68
|
|
|
if (!is_array($my_arr) || is_null($elem)) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
array_splice($my_arr, $count - 1); |
72
|
|
|
array_unshift($my_arr, $elem); // Shift every element down one, and insert new at top |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!function_exists('array_has')) { |
78
|
|
|
/** |
79
|
|
|
* Check if an item exists in an array using "dot" notation. |
80
|
|
|
* |
81
|
|
|
* @param array $array |
82
|
|
|
* @param string $key |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
function array_has($array, $key) |
86
|
|
|
{ |
87
|
|
|
if (empty($array) || is_null($key)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
if (array_key_exists($key, $array)) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
94
|
|
|
if (!array_key_exists_safe($segment, $array)) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
$array = $array[$segment]; |
98
|
|
|
} |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!function_exists('array_accessible')) { |
104
|
|
|
/** |
105
|
|
|
* Determine whether the given value is array accessible. |
106
|
|
|
* See: https://github.com/illuminate/support/blob/master/Arr.php |
107
|
|
|
* |
108
|
|
|
* @param mixed $value |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
function array_accessible($value) |
112
|
|
|
{ |
113
|
|
|
return is_array($value) || $value instanceof ArrayAccess; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!function_exists('array_get')) { |
118
|
|
|
/** |
119
|
|
|
* Get an item from an array using "dot" notation. |
120
|
|
|
* |
121
|
|
|
* @param array $array |
122
|
|
|
* @param string $key |
123
|
|
|
* @param mixed $default |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
function array_get($array, $key, $default = null) |
127
|
|
|
{ |
128
|
|
|
if (!array_accessible($array)) { |
129
|
|
|
return value($default); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (is_null($key)) { |
133
|
|
|
return $array; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (array_key_exists_safe($array, $key)) { |
137
|
|
|
return $array[$key]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (strpos($key, '.') === false) { |
141
|
|
|
return $array[$key] ?? value($default); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
foreach (explode('.', $key) as $segment) { |
145
|
|
|
if (!array_accessible($array) || !array_key_exists_safe($array, $segment)) { |
146
|
|
|
return value($default); |
147
|
|
|
} |
148
|
|
|
$array = $array[$segment]; |
149
|
|
|
} |
150
|
|
|
return $array; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!function_exists('array_set')) { |
155
|
|
|
/** |
156
|
|
|
* Set an array item to a given value using "dot" notation. |
157
|
|
|
* If no key is given to the method, the entire array will be replaced. |
158
|
|
|
* |
159
|
|
|
* @see: https://github.com/illuminate/support/blob/master/Arr.php |
160
|
|
|
* |
161
|
|
|
* @param array $array |
162
|
|
|
* @param string|null $key |
163
|
|
|
* @param mixed $value |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
function array_set(&$array, $key, $value) |
167
|
|
|
{ |
168
|
|
|
if (is_null($key)) { |
169
|
|
|
return $array = $value; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$keys = explode('.', $key); |
173
|
|
|
|
174
|
|
|
foreach ($keys as $i => $key) { |
175
|
|
|
if (count($keys) === 1) { |
176
|
|
|
break; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
unset($keys[$i]); |
180
|
|
|
|
181
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
182
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
183
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
184
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
185
|
|
|
$array[$key] = []; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$array = &$array[$key]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$array[array_shift($keys)] = $value; |
192
|
|
|
|
193
|
|
|
return $array; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Return an array with only integers value contained in the array passed |
199
|
|
|
* @param array $array |
200
|
|
|
* @return array |
201
|
|
|
**/ |
202
|
|
|
function CleanUpArrayOfInt($array) |
203
|
|
|
{ |
204
|
|
|
$result = array(); |
205
|
|
|
if (isNullOrEmptyArray($array)) { |
206
|
|
|
return $result; |
207
|
|
|
} |
208
|
|
|
reset($array); |
209
|
|
|
while (list($key, $value) = each($array)) { |
|
|
|
|
210
|
|
|
if (isInteger($value)) { |
211
|
|
|
$result[] = $value; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
reset($array); |
215
|
|
|
|
216
|
|
|
return $result; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (!function_exists('array_split_filter')) { |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns an array with two elements. |
223
|
|
|
* |
224
|
|
|
* Iterates over each value in the array passing them to the callback function. |
225
|
|
|
* If the callback function returns true, the current value from array is returned in the first |
226
|
|
|
* element of result array. If not, it is return in the second element of result array. |
227
|
|
|
* |
228
|
|
|
* Array keys are preserved. |
229
|
|
|
* |
230
|
|
|
* @param array $array |
231
|
|
|
* @param callable $callback |
232
|
|
|
* @return array |
233
|
|
|
* @see https://github.com/spatie/array-functions/blob/master/src/array_functions.php |
234
|
|
|
*/ |
235
|
|
|
function array_split_filter(array $array, callable $callback) |
236
|
|
|
{ |
237
|
|
|
$passesFilter = array_filter($array, $callback); |
238
|
|
|
$negatedCallback = function ($item) use ($callback) { |
239
|
|
|
return !$callback($item); |
240
|
|
|
}; |
241
|
|
|
$doesNotPassFilter = array_filter($array, $negatedCallback); |
242
|
|
|
return [$passesFilter, $doesNotPassFilter]; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if (!function_exists('in_array_column')) { |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Checks whether specific value exists in array of object. |
250
|
|
|
* For exampe, following code |
251
|
|
|
* $exist = in_array_column([['id' => 1], ['id' => 2], ['id' => 3]], 3, 'id'); |
252
|
|
|
* will produce 2 |
253
|
|
|
* @param array $haystack Source array |
254
|
|
|
* @param mixed $needle Needed value |
255
|
|
|
* @param string $column Column to perform search |
256
|
|
|
* @param bool $strict Should search be strict or not. |
257
|
|
|
* @return bool True if value exists in array, False otherwise. |
258
|
|
|
* @since 2015.05.19 |
259
|
|
|
* @author wapmorgan |
260
|
|
|
* @see modified from https://github.com/wapmorgan/php-functions-repository/blob/master/i/in_array_column.php |
261
|
|
|
*/ |
262
|
|
|
function in_array_column($haystack, $needle, $column, $strict = false) |
263
|
|
|
{ |
264
|
|
|
foreach ($haystack as $k => $elem) { |
265
|
|
|
if ((!$strict && $elem[$column] == $needle) || ($strict && $elem[$column] === $needle)) { |
266
|
|
|
return true; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
return false; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if (!function_exists('objectToArray')) { |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Convert objecte to the array. |
277
|
|
|
* |
278
|
|
|
* @param $object |
279
|
|
|
* |
280
|
|
|
* @return array |
281
|
|
|
* @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php |
282
|
|
|
*/ |
283
|
|
|
function objectToArray($object): array |
284
|
|
|
{ |
285
|
|
|
if (!is_object($object) && !is_array($object)) { |
286
|
|
|
return []; |
287
|
|
|
} |
288
|
|
|
if (is_object($object)) { |
289
|
|
|
$object = get_object_vars($object); |
290
|
|
|
} |
291
|
|
|
return array_map('objectToArray', $object); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if (!function_exists('arrayToObject')) { |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Convert array to the object. |
299
|
|
|
* |
300
|
|
|
* @param array $array PHP array |
301
|
|
|
* |
302
|
|
|
* @return stdClass (object) |
303
|
|
|
* @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php |
304
|
|
|
*/ |
305
|
|
|
function arrayToObject(array $array = []): \stdClass |
306
|
|
|
{ |
307
|
|
|
$object = new \stdClass(); |
308
|
|
|
|
309
|
|
|
if (!is_array($array) || count($array) < 1) { |
310
|
|
|
return $object; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
foreach ($array as $name => $value) { |
314
|
|
|
if (is_array($value)) { |
315
|
|
|
$object->$name = arrayToObject($value); |
316
|
|
|
continue; |
317
|
|
|
} |
318
|
|
|
$object->{$name} = $value; |
319
|
|
|
} |
320
|
|
|
return $object; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
if (!function_exists('arrayToString')) { |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Convert Array to string |
328
|
|
|
* expected output: <key1>="value1" <key2>="value2". |
329
|
|
|
* |
330
|
|
|
* @param array $array array to convert to string |
331
|
|
|
* |
332
|
|
|
* @return string |
333
|
|
|
* @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php |
334
|
|
|
*/ |
335
|
|
|
function arrayToString(array $array = []): string |
336
|
|
|
{ |
337
|
|
|
if (isNullOrEmptyArray($array)) { |
338
|
|
|
return ''; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$string = ''; |
342
|
|
|
foreach ($array as $key => $value) { |
343
|
|
|
$string .= $key . '="' . $value . '" '; |
344
|
|
|
} |
345
|
|
|
return rtrim($string, ' '); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
if (!function_exists('array_key_exists_safe')) { |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Check if a key exists in array |
353
|
|
|
* @param array $array |
354
|
|
|
* @param string $key |
355
|
|
|
* @return bool |
356
|
|
|
*/ |
357
|
|
|
function array_key_exists_safe(array $array, string $key): bool |
358
|
|
|
{ |
359
|
|
|
if (isNullOrEmptyArray($array) || isNullOrEmpty($key)) { |
360
|
|
|
return false; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return array_key_exists($key, $array); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if (!function_exists('array_get_key_value_safe')) { |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Retrieve a single key from an array. |
371
|
|
|
* If the key does not exist in the array, or array is null or empty |
372
|
|
|
* the default value will be returned instead. |
373
|
|
|
* @param array $array |
374
|
|
|
* @param string $key |
375
|
|
|
* @param string $default |
376
|
|
|
* @return mixed |
377
|
|
|
*/ |
378
|
|
|
function array_get_key_value_safe(array $array, string $key, $default = null) |
379
|
|
|
{ |
380
|
|
|
if (isNullOrEmptyArray($array) || isNullOrEmpty($key) || !array_key_exists($key, $array)) { |
381
|
|
|
return $default; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $array[$key]; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
if (!function_exists('isNullOrEmptyArray')) { |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Check if array is null or empty. |
392
|
|
|
* @param $array |
393
|
|
|
* @return bool |
394
|
|
|
*/ |
395
|
|
|
function isNullOrEmptyArray($array): bool |
396
|
|
|
{ |
397
|
|
|
return $array === null || !is_array($array) || count($array) < 1; |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
if (!function_exists('isNullOrEmptyArrayKey')) { |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Check if an array key not exits or exists and is null or empty. |
405
|
|
|
* @param $array |
406
|
|
|
* @param string $key |
407
|
|
|
* @param bool $withTrim if set to true (default) check if trim()!='' too. |
408
|
|
|
* @return bool |
409
|
|
|
*/ |
410
|
|
|
function isNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true): bool |
411
|
|
|
{ |
412
|
|
|
return !array_key_exists_safe($array, $key) || $array[$key] === null || isNullOrEmpty($array[$key], $withTrim); |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
if (!function_exists('isNotNullOrEmptyArray')) { |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Check if array is not null and not empty. |
420
|
|
|
* @param $array |
421
|
|
|
* @return bool |
422
|
|
|
*/ |
423
|
|
|
function isNotNullOrEmptyArray($array): bool |
424
|
|
|
{ |
425
|
|
|
return !isNullOrEmptyArray($array); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
if (!function_exists('isNotNullOrEmptyArrayKey')) { |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Check if an array key exists and is not null and not empty. |
433
|
|
|
* @param $array |
434
|
|
|
* @param string $key |
435
|
|
|
* @param bool $withTrim if set to true (default) check if trim()!='' too. |
436
|
|
|
* @return bool |
437
|
|
|
*/ |
438
|
|
|
function isNotNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true): bool |
439
|
|
|
{ |
440
|
|
|
return !isNullOrEmptyArrayKey($array, $key, $withTrim); |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
if (!function_exists('array_remove_columns')) { |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Remove given column from the subarrays of a two dimensional array. |
448
|
|
|
* @param $array |
449
|
|
|
* @param int $columnToRemove |
450
|
|
|
* @return array |
451
|
|
|
*/ |
452
|
|
|
function array_remove_columns(array $array, int $columnToRemove): array |
453
|
|
|
{ |
454
|
|
|
if (count($array) < 1) { |
455
|
|
|
return []; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$numCol = count($array[0]); |
459
|
|
|
|
460
|
|
|
if ($columnToRemove == 1 && isInRange($numCol, 0, 1)) { |
461
|
|
|
return []; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
foreach ($array as &$element) { |
465
|
|
|
if ($columnToRemove == 1) { |
466
|
|
|
$element = array_slice($element, 1, $numCol - 1); |
467
|
|
|
} elseif ($columnToRemove == $numCol) { |
468
|
|
|
$element = array_slice($element, 0, $numCol - 1); |
469
|
|
|
} else { |
470
|
|
|
$element1 = array_slice($element, 0, $columnToRemove - 1); |
471
|
|
|
$element2 = array_slice($element, $columnToRemove, $numCol - $columnToRemove); |
472
|
|
|
$element = array_merge($element1, $element2); |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
if (!is_array($array)) { |
477
|
|
|
return []; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
return $array; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
if (!function_exists('array_remove_first_columns')) { |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Remove first column from the subarrays of a two dimensional array. |
488
|
|
|
* @param $array |
489
|
|
|
* @return array |
490
|
|
|
*/ |
491
|
|
|
function array_remove_first_columns(array $array): array |
492
|
|
|
{ |
493
|
|
|
return array_remove_columns($array, 1); |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
if (!function_exists('array_remove_last_columns')) { |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Remove last column from the subarrays of a two dimensional array. |
501
|
|
|
* @param $array |
502
|
|
|
* @return array |
503
|
|
|
*/ |
504
|
|
|
function array_remove_last_columns(array $array): array |
505
|
|
|
{ |
506
|
|
|
if (count($array) < 1) { |
507
|
|
|
return []; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
$numCol = count($array[0]); |
511
|
|
|
|
512
|
|
|
return array_remove_columns($array, $numCol); |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.