This repository was archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArr.php
909 lines (794 loc) · 34.4 KB
/
Arr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<?php namespace nyx\utils;
// External dependencies
use nyx\diagnostics;
/**
* Arr
*
* The Arr class provides a few helper methods to make dealing with arrays easier.
*
* All methods which work with string delimited keys accept a string delimiter. If none is given (ie. null is passed),
* the default delimiter (dot) set statically in this class will be used.
*
* Some code in this class can be simplified and some duplication could be avoided but it is laid out so that the
* most common use cases are checked for first with performance being the priority.
*
* Some methods have aliases. To avoid the overhead please use the base methods, not the aliases. The methods which
* have aliases are documented as such and each alias directly points to the base method.
*
* Note: This class is based on Laravel, FuelPHP, Lo-dash and a few others, but certain methods which appear in
* those are not included since they would add overhead we consider 'not worth it' and don't want to encourage
* the use thereof:
*
* - Arr::each() -> use array_map() instead.
* - Arr::filter(), Arr::reject() -> use array_filter() instead.
* - Arr::range() -> use range() instead.
* - Arr::repeat() -> use array_fill() instead.
* - Arr::search() -> use array_search() instead.
* - Arr::shuffle() -> use shuffle() instead.
* - Arr::size() -> use count() instead.
*
* @package Nyx\Utils
* @version 0.1.0
* @author Michal Chojnacki <[email protected]>
* @copyright 2012-2016 Nyx Dev Team
* @link http://docs.muyo.io/nyx/utils/index.html
* @todo Arr::sort() and Arr:sortBy() (add sortBy() to core\traits\Collection as well).
* @todo Add ArrayObject support? How? Just strip the array type hints so as to not add overhead with checks?
*/
class Arr
{
/**
* The traits of the Arr class.
*/
use traits\StaticallyExtendable;
/**
* @var string The default delimiter to use to separate array dimensions.
*/
public static $delimiter = '.';
/**
* Adds an element to the given array but only if it does not yet exist.
*
* Note: Null as value of an item is considered a non-existing item for the purposes
* of this method.
*
* @param array $array The array to which the element should be added.
* @param string $key The key at which the value should be added.
* @param mixed $value The value of the element.
* @param string $delimiter The delimiter to use when exploding the key into parts.
*/
public static function add(array& $array, string $key, $value, string $delimiter = null)
{
if (null === static::get($array, $key, null, $delimiter)) {
static::set($array, $key, $value, $delimiter);
}
}
/**
* Checks whether all elements in the given array pass the given truth test.
*
* Aliases:
* - @see Arr::every()
*
* @param array $array The array to traverse.
* @param callable $callback The truth test the elements should pass.
* @return bool True when the elements passed the truth test, false otherwise.
*/
public static function all(array $array, callable $callback) : bool
{
// Map the array and then search for a 'false' boolean. If none is found,
// we assume all elements passed the test.
return !in_array(false, array_map($callback, $array), true);
}
/**
* Checks whether any of the elements in the given array passes the given truth test.
*
* Aliases:
* - @see Arr::some()
*
* @param array $array The array to traverse.
* @param callable $callback The truth test the elements should pass.
* @return bool True when at least on the the elements passed the truth test, false
* otherwise.
*/
public static function any(array $array, callable $callback) : bool
{
// Map the array and then search for a 'true' boolean. If at least one is found,
// we assume at least one element passed the test.
return in_array(true, array_map($callback, $array), true);
}
/**
* Returns the average value of the given array.
*
* @param array $array The array to traverse.
* @param int $decimals The number of decimals to return.
* @return float The average value.
*/
public static function average(array $array, int $decimals = 0) : float
{
return round((array_sum($array) / count($array)), $decimals);
}
/**
* Removes all elements containing falsy values from the given array. The keys are preserved.
*
* See {@link http://php.net/manual/en/language.types.boolean.php} for information on which values evaluate
* to false.
*
* @param array $array The array to traverse.
* @return array The resulting array.
*/
public static function clean(array $array) : array
{
return array_filter($array, function($value) {
return (bool) $value;
});
}
/**
* Collapses an array of arrays into a single array. Not recursive, ie. only the first dimension of arrays
* will be collapsed down.
*
* Standard array_merge() rules apply - @link http://php.net/manual/en/function.array-merge.php -
* non-array values with numeric keys will be appended to the resulting array in the order they are given, while
* non-array values with non-numeric keys will have their keys preserved but the values may be overwritten by
* the nested arrays being collapsed down if those contain values with the same non-numeric keys. Latter arrays
* overwrite previous arrays' keys on collisions.
*
* @param array $array The array to collapse.
* @return array The resulting array.
*/
public static function collapse(array $array) : array
{
$results = [];
foreach ($array as $key => $item) {
// Nested arrays will be merged in (non-recursively).
if (is_array($item)) {
$results = array_merge($results, $item); continue;
}
// Values with numeric keys will be appended in any case.
if (is_int($key)) {
$results[] = $item; continue;
}
// Non-numeric keys. If we've got the given key in $results already, it means it was merged
// in from one of the nested arrays and those are meant to overwrite the initial values on collisions -
// thus we're not going to do anything in that case.
if (!array_key_exists($key, $results)) {
$results[$key] = $item;
}
}
return $results;
}
/**
* Checks whether the given value is contained within the given array. Equivalent of a recursive in_array.
* When you are sure you are dealing with a 1-dimensional array, use in_array instead to avoid the overhead.
*
* @param array $haystack The array to search in.
* @param mixed $needle The value to search for.
* @param bool $strict Whether strict equality matches should be performed on the values.
* @return bool True when the value was found in the array, false otherwise.
*/
public static function contains(array $haystack, $needle, bool $strict = true)
{
foreach ($haystack as $value) {
if ((!$strict && $needle == $value) || $needle === $value) {
return true;
}
if (is_array($value) && static::contains($needle, $value, $strict)) {
return true;
}
}
return false;
}
/**
* Flattens a multi-dimensional array using the given delimiter.
*
* @param array $array The initial array.
* @param string $prepend A string that should be prepended to the keys.
* @param string $delimiter The delimiter to use when exploding the key into parts.
* @return array The resulting array.
*/
public static function delimit(array $array, string $prepend = '', string $delimiter = null)
{
// Results holder.
$results = [];
// Which string delimiter should we use?
if (null === $delimiter) {
$delimiter = static::$delimiter;
}
foreach ($array as $key => $value) {
if (is_array($value) && !empty($value)) {
$results = array_merge($results, static::delimit($value, $prepend.$key.$delimiter));
} else {
$results[$prepend.$key] = $value;
}
}
return $results;
}
/**
* Alias for @see Arr::find()
*/
public static function detect(array $array, callable $callback, $default = null)
{
return static::find($array, $callback, $default);
}
/**
* Divides an array into two arrays - the first containing the keys, the second containing the values.
*
* @param array $array The initial array.
* @return array The resulting array.
*/
public static function divide(array $array)
{
return [array_keys($array), array_values($array)];
}
/**
* Alias for @see Arr::all()
*/
public static function every(array $array, callable $callback)
{
return static::all($array, $callback);
}
/**
* Returns a subset of the given array, containing all keys except for the ones specified.
*
* @param array $array The initial array.
* @param array $keys An array of keys to exclude from the initial array.
* @return array
*/
public static function except(array $array, array $keys)
{
return array_diff_key($array, array_flip($keys));
}
/**
* Fetches a flattened array of an element nested in the initial array.
*
* @param array $array The initial array.
* @param string $key The string delimited key.
* @param string $delimiter The delimiter to use when exploding the key into parts.
* @return array The resulting array.
*/
public static function fetch(array $array, string $key, string $delimiter = null) : array
{
// Results holder.
$results = [];
// Which string delimiter should we use?
if (null === $delimiter) {
$delimiter = static::$delimiter;
}
foreach (explode($delimiter, $key) as $segment) {
$results = [];
foreach ($array as $value) {
$value = (array) $value;
$results[] = $value[$segment];
}
$array = array_values($results);
}
return array_values($results);
}
/**
* Returns the first value which passes the given truth test.
*
* Aliases:
* - @see Arr::detect()
*
* @param array $array The array to traverse.
* @param callable $callback The truth test the value should pass.
* @param mixed $default The default value to be returned if none of the elements passes the test.
* @return mixed
*/
public static function find(array $array, callable $callback, $default = null)
{
foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return $value;
}
}
return $default;
}
/**
* Returns the first element of the array,
* OR the first $elements of the array when $elements is a positive integer,
* OR the first element which passes the given truth test when $elements is a callable.
*
* Aliases: @see \nyx\utils\Arr::head(), \nyx\utils\Arr::take()
* Opposite: @see \nyx\utils\Arr::last()
*
* @param array $array The array to traverse.
* @param callable|int $elements The truth test the value should pass or an integer denoting how many
* of the initial elements of the array should be returned.
* When not given, the method will return the first element of the array.
* @param mixed $default The default value to be returned if none of the elements passes
* the test or the array is empty.
* @throws \InvalidArgumentException When $elements is an integer smaller than 1.
* @throws \InvalidArgumentException When $elements is neither a valid integer nor a callable.
* @return mixed
*/
public static function first(array $array, $elements = null, $default = null)
{
if (empty($array)) {
return $default;
}
// Most common use case - simply return the first value of the array.
if (!isset($elements) || $elements === 1) {
return reset($array);
}
// With a integer given, return a slice containing the first $callback elements.
if (is_int($elements)) {
if ($elements < 1) {
throw new \InvalidArgumentException("At least 1 element must be requested, while [$elements] were requested.");
}
return array_slice($array, 0, $elements);
}
// With a callable given, return the first value which passes the given truth test.
if (is_callable($elements)) {
return static::find($array, $elements, $default);
}
throw new \InvalidArgumentException('Expected $callback to be a positive integer or a callable, got ['.diagnostics\Debug::getTypeName($elements).'] instead.');
}
/**
* Flattens a multi-dimensional array.
*
* @param array $array The initial array.
* @return array The flattened array.
*/
public static function flatten(array $array)
{
$results = [];
array_walk_recursive($array, function($x) use (&$results) {
$results[] = $x;
});
return $results;
}
/**
* Returns a string delimited key from an array, with a default value if the given key does not exist. If null
* is given instead of a key, the whole initial array will be returned.
*
* Note: Nested objects will be accessed as if they were arrays, eg. if "some.nested.object" is an object,
* then looking for "some.nested.object.property" will be handled just as a normal array would.
*
* @param array $array The array to search in.
* @param string|array $key The string delimited key or a chain (array) of nested keys pointing
* to the desired key.
* @param mixed $default The default value.
* @param string $delimiter The delimiter to use when exploding the key into parts.
* @return mixed
*/
public static function get(array $array, $key = null, $default = null, string $delimiter = null)
{
// Make loops easier for the end-user - return the initial array if the key is null instead of forcing
// a valid value.
if (!isset($key)) {
return $array;
}
// Which string delimiter should we use?
if (!isset($delimiter)) {
$delimiter = static::$delimiter;
}
// If the key is string delimited, we need to explode it into an array of segments.
$segments = is_array($key) ? $key : explode($delimiter, $key);
// One dimension at a time.
while ($segment = array_shift($segments)) {
// If the current segment is a wildcard, make sure the it points to an array
// and pluck the remaining segments from it.
if ($segment === '*') {
return is_array($array) ? static::pluck($array, $segments, $delimiter) : $default;
}
// Note: isset() is the cheapest condition to check for while being rather probable at the same time,
// thus the seemingly unintuitive condition ordering.
if (isset($array->{$segment})) {
$array = $array->{$segment};
} elseif (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return $default;
}
}
return $array;
}
/**
* Checks whether the given string delimited key exists in the array.
*
* Note: Nested objects will not be accessed as if they were arrays, ie. if "some.nested.object" is an object,
* not an array, then looking for "some.nested.object.key" will always return false.
*
* @param array $array The array to search in.
* @param string $key The string delimited key.
* @param string $delimiter The delimiter to use when exploding the key into parts.
* @return bool True when the given key exists, false otherwise.
*/
public static function has(array $array, string $key, string $delimiter = null)
{
// Which string delimiter should we use?
if (null === $delimiter) {
$delimiter = static::$delimiter;
}
foreach (explode($delimiter, $key) as $segment) {
if (!is_array($array) || !array_key_exists($segment, $array)) {
return false;
}
$array = $array[$segment];
}
return true;
}
/**
* @see \nyx\utils\Arr::first()
*/
public static function head(array $array, $callback = null, $default = null)
{
return static::first($array, $callback, $default);
}
/**
* Returns all but the last value(s) of the given array.
*
* If a callable is passed, elements at the end of the array are excluded from the result as long as the
* callback returns a truthy value. If a number is passed, the last n values are excluded from the result.
*
* @param array $array The array to traverse.
* @param callable|int $callback The truth test the value should pass or an integer denoting how many
* of the final elements of the array should be excluded. The count is
* 1-indexed, ie. if you want to exclude the last 2 elements, pass 2.
* @param mixed $default The default value to be returned if none of the elements passes the test.
* Only useful when $callback is a callable.
* @return mixed
*/
public static function initial(array $array, $callback, $default = null)
{
// When given a callable, keep counting as long as the callable returns a truthy value.
if (is_callable($callback)) {
$i = 0;
foreach (array_reverse($array) as $key => $value) {
if (!$callback($value, $key)) {
break;
}
$i++;
}
// If we didn't get at least a single truthy value, return the default.
if ($i === 0) {
return $default;
}
// Otherwise we're just gonna overwrite the $callback and proceed as if it were an integer in the
// first place.
$callback = $i;
}
// At this point we need a positive integer, 1 at minimum.
return array_slice($array, 0, -(int) (!$callback ? 1 : abs($callback)));
}
/**
* Checks whether the given array is an associative array.
*
* @param array $array The array to check.
* @return bool True when the array is associative, false otherwise.
*/
public static function isAssociative(array $array) : bool
{
return array_keys($array) !== range(0, count($array) - 1);
}
/**
* Checks whether the given array is a multidimensional array.
*
* @param array $array The array to check.
* @return bool True when the array has multiple dimensions, false otherwise.
*/
public static function isMultidimensional(array $array) : bool
{
return count($array) !== count($array, COUNT_RECURSIVE);
}
/**
* Returns the last element of the array,
* OR the last $elements of the array when $elements is a positive integer,
* OR the last element which passes the given truth test when $elements is a callable.
*
* Opposite: @see \nyx\utils\Arr::first()
*
* @param array $array The array to traverse.
* @param callable|int $elements The truth test the value should pass or a positive integer
* denoting how many of the final elements of the array should be returned.
* When not given, the method will return the first element of the array.
* @param mixed $default The default value to be returned if none of the elements passes
* the test or the array is empty.
* @throws \InvalidArgumentException When $elements is an integer smaller than 1.
* @throws \InvalidArgumentException When $elements is neither a valid integer nor a callable.
* @return mixed
*/
public static function last(array $array, $elements = null, $default = null)
{
if (empty($array)) {
return $default;
}
// Most common use case - simply return the last value of the array.
if (!isset($elements) || $elements === 1) {
return end($array);
}
// With a integer given, return a slice containing the last $elements elements.
if (is_int($elements)) {
if ($elements < 1) {
throw new \InvalidArgumentException("At least 1 element must be requested, while [$elements] were requested.");
}
return array_slice($array, -$elements);
}
// With a callable given, return the last value which passes the given truth test.
if (is_callable($elements)) {
return static::find(array_reverse($array), $elements, $default);
}
throw new \InvalidArgumentException('Expected $elements to be a positive integer or a callable, got ['.diagnostics\Debug::getTypeName($elements).'] instead.');
}
/**
* Returns the biggest value from the given array.
*
* @param array $array The array to traverse.
* @return mixed The resulting value.
*/
public static function max(array $array)
{
// Avoid some overhead at this point already if possible.
if (empty($array)) {
return null;
}
// Sort in a descending order.
arsort($array);
// Return the first element of the sorted array.
return reset($array);
}
/**
* Merges 2 or more arrays recursively. Differs in two important aspects from array_merge_recursive(), to
* more closely resemble the standard behaviour of the non-recursive array_merge():
* - In case of 2 different values, when they are not arrays, the latter one overwrites the earlier instead
* of merging them into an array;
* - Non-conflicting numeric keys are left unchanged. In case of a conflict, the new value will be appended
* to the resulting array (not preserving its key).
*
* @param array $array The initial array.
* @param array ...$with One or more (ie. separate arguments) arrays to merge in.
* @return array The resulting merged array.
*/
public static function merge(array $array, array ...$with) : array
{
foreach ($with as $arr) {
foreach ($arr as $key => $value) {
// Append numeric keys.
if (is_int($key)) {
array_key_exists($key, $array) ? $array[] = $value : $array[$key] = $value;
}
// Merge multi-dimensional arrays recursively.
elseif (is_array($value) && array_key_exists($key, $array) && is_array($array[$key])) {
$array[$key] = static::merge($array[$key], $value);
} else {
$array[$key] = $value;
}
}
}
return $array;
}
/**
* Returns the smallest value from the given array.
*
* @param array $array The array to traverse.
* @return mixed The resulting value.
*/
public static function min(array $array)
{
// Avoid some overhead at this point already if possible.
if (empty($array)) {
return null;
}
// Sort in an ascending order.
asort($array);
// Return the first element of the sorted array.
return reset($array);
}
/**
* Returns a subset of the given array, containing only the specified keys.
*
* @param array $array The initial array.
* @param array $keys An array of keys (the keys are expected to be values of this array).
* @return array
*/
public static function only(array $array, array $keys) : array
{
return array_intersect_key($array, array_values($keys));
}
/**
* Returns a random value from the given array, or $elements random values when $elements is a positive integer, or
* the first random element that passes the given truth test when $elements is a callable.
*
* @param array $array The array to search in.
* @param callable|int $elements The number of random values to return or a callable to return the first
* randomly shuffled element that passes the given truth test.
* @param mixed $default The default value to be returned if none of the elements passes
* the test or the array is empty.
* @return mixed
*/
public static function pick(array $array, $elements = null, $default = null)
{
// There are *slightly* better performing alternatives, but simply shuffling and delegating
// the actual picking to static::first() allows us to avoid a fair amount of duplicated code.
shuffle($array);
return static::first($array, $elements, $default);
}
/**
* Given an array containing other arrays or objects, looks for the value with the given key/property
* of $key within them and returns a new array containing all values of said key from the initial array.
* Essentially like fetching a single column from a classic database table.
*
* When the optional $index parameter is given, the resulting array will be indexed by the values corresponding
* to the given $index.
*
* @see array_column() A faster and simpler alternative, if you do not need to pluck data with support
* for delimited keys or wildcards.
*
* @param array $array The array to search in.
* @param string|array $key The key of the value to look for.
* @param string|array $index The key of the value to index the resulting array by.
* @param string $delimiter The delimiter to use when exploding the key into parts.
* @return array
*/
public static function pluck(array $array, $key, $index = null, string $delimiter = null) : array
{
$results = [];
// Which string delimiter should we use?
if (!isset($delimiter)) {
$delimiter = static::$delimiter;
}
foreach ($array as $item) {
$itemValue = static::get($item, $key, $delimiter);
// If the key given is null, the resulting array will contain numerically indexed keys.
if (!isset($index)) {
$results[] = $itemValue;
}
// Otherwise we are going use the value of the given key and use it in the resulting array as key
// for the value determined earlier.
else {
$results[static::get($item, $index, $delimiter)] = $itemValue;
}
}
return $results;
}
/**
* Returns the value for a string delimited key from an array and then removes it.
*
* @param array $array The array to search in.
* @param string $key The string delimited key.
* @param string $delimiter The delimiter to use when exploding the key into parts.
* @return mixed
*/
public static function pull(&$array, string $key, string $delimiter = null)
{
$value = static::get($array, $key, null, $delimiter);
static::remove($array, $key, $delimiter);
return $value;
}
/**
* Removes a string delimited key from the given array.
*
* @param array& $array The array to search in.
* @param string $key The string delimited key.
* @param string $delimiter The delimiter to use when exploding the key into parts.
*/
public static function remove(array& $array, string $key, string $delimiter = null)
{
// Which string delimiter should we use?
if (!isset($delimiter)) {
$delimiter = static::$delimiter;
}
// Explode the key according to that delimiter.
$keys = explode($delimiter, $key);
while ($key = array_shift($keys)) {
if (!isset($array[$key]) || !is_array($array[$key])) {
return;
}
$array =& $array[$key];
}
unset($array[array_shift($keys)]);
}
/**
* Returns all but the first value of the given array, all but the first elements for which the $callback
* returns true if $callback is a callable, or all but the first $callback elements if $callback is a number.
*
* Aliases:
* - @see Arr::tail()
*
* @param array $array The array to traverse.
* @param callable|int|bool $callback The truth test the value should pass or an integer denoting how many
* of the initial elements of the array should be excluded. The count
* is 1-indexed, ie. if you want to exclude the first 2 elements, pass 2.
* When a falsy value is given, the method will return all but the first
* element of the array.
* @param mixed $default The default value to be returned if none of the elements passes the
* test or the array contains no more than one item.
* @return mixed
*/
public static function rest(array $array, $callback = false, $default = null)
{
// Avoid some overhead at this point already if possible. We need at least 2 elements in the array for
// this method to make any usage sense.
if (2 > count($array)) {
return $default;
}
// For a falsy callback, return all but the first element of the array.
if (!$callback) {
return array_slice($array, 1);
}
// With a callable given, keep counting as long as the callable returns a truthy value.
if (is_callable($callback)) {
$i = 0;
foreach ($array as $key => $value) {
if (!$callback($value, $key)) {
break;
}
$i++;
}
// If we didn't get at least a single truthy value, return the default.
if ($i === 0) {
return $default;
}
// Otherwise we're just gonna overwrite the $callback and proceed as if it were an integer in the
// first place.
$callback = $i;
}
// Return the final $callback elements.
return array_slice($array, abs((int) $callback));
}
/**
* Sets the given value for a string delimited key within the given array. If null is given instead of a key,
* the whole initial array will be overwritten with the given value.
*
* @param array $array The array to set the value in.
* @param string $key The string delimited key.
* @param mixed $value The value to set.
* @param string $delimiter The delimiter to use when exploding the key into parts.
* @return mixed
*/
public static function set(array& $array, $key, $value, string $delimiter = null)
{
// Make loops easier for the end-user - overwrite the whole array if the key is null.
if (null === $key) {
return $array = $value;
}
// Which string delimiter should we use?
if (null === $delimiter) {
$delimiter = static::$delimiter;
}
// Explode the key according to that delimiter.
$keys = explode($delimiter, $key);
while (count($keys) > 1) {
$key = array_shift($keys);
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array =& $array[$key];
}
return $array[array_shift($keys)] = $value;
}
/**
* Alias for {@see static::any()}
*/
public static function some(array $array, callable $callback) : bool
{
return static::any($array, $callback);
}
/**
* Alias for {@see static::rest()}
*/
public static function tail(array $array, $callback = false, $default = null)
{
return static::rest($array, $callback, $default);
}
/**
* @see \nyx\utils\Arr::first()
*/
public static function take(array $array, $callback = null, $default = null)
{
return static::first($array, $callback, $default);
}
/**
* Returns an array based on the initial array with all occurrences of the passed values removed. Uses strict
* equality comparisons.
*
* @param array $array The array to traverse.
* @param mixed ...$values The values which should get removed.
* @return array
*/
public static function without(array $array, ...$values) : array
{
return array_filter($array, function($value) use ($values) {
return !in_array($value, $values, true);
});
}
}