@@ -43,12 +43,14 @@ composer require bayfrontmedia/php-array-helpers
43
43
- [ missing] ( #missing )
44
44
- [ isMissing] ( #ismissing )
45
45
- [ multisort] ( #multisort )
46
+ - [ numericMultisort] ( #numericmultisort )
46
47
- [ renameKeys] ( #renamekeys )
47
48
- [ order] ( #order )
48
49
- [ query] ( #query )
49
50
- [ getAnyValues] ( #getanyvalues )
50
51
- [ hasAnyValues] ( #hasanyvalues )
51
52
- [ hasAllValues] ( #hasallvalues )
53
+ - [ ensureHas] ( #ensurehas )
52
54
53
55
<hr />
54
56
@@ -72,8 +74,6 @@ The key values will never be an array, even if empty. Empty arrays will be dropp
72
74
** Example:**
73
75
74
76
```
75
- use Bayfront\ArrayHelpers\Arr;
76
-
77
77
$array = [
78
78
'name' => [
79
79
'first_name' => 'John',
@@ -106,8 +106,6 @@ Converts array in "dot" notation to a standard multidimensional array.
106
106
** Example:**
107
107
108
108
```
109
- use Bayfront\ArrayHelpers\Arr;
110
-
111
109
$array = [
112
110
'name.first_name' => 'John',
113
111
'name.last_name' => 'Doe'
@@ -137,8 +135,6 @@ Set an array item to a given value using "dot" notation.
137
135
** Example:**
138
136
139
137
```
140
- use Bayfront\ArrayHelpers\Arr;
141
-
142
138
$array = [
143
139
'name' => [
144
140
'first_name' => 'John',
@@ -169,8 +165,6 @@ Checks if array key exists and not null using "dot" notation.
169
165
** Example:**
170
166
171
167
```
172
- use Bayfront\ArrayHelpers\Arr;
173
-
174
168
$array = [
175
169
'name' => [
176
170
'first_name' => 'John',
@@ -204,8 +198,6 @@ Get an item from an array using "dot" notation, returning an optional default va
204
198
** Example:**
205
199
206
200
```
207
- use Bayfront\ArrayHelpers\Arr;
208
-
209
201
$array = [
210
202
'name' => [
211
203
'first_name' => 'John',
@@ -237,8 +229,6 @@ Returns an array of values for a given key from an array using "dot" notation.
237
229
** Example:**
238
230
239
231
```
240
- use Bayfront\ArrayHelpers\Arr;
241
-
242
232
$array = [
243
233
[
244
234
'user_id' => 110,
@@ -275,8 +265,6 @@ Remove a single key, or an array of keys from a given array using "dot" notation
275
265
** Example:**
276
266
277
267
```
278
- use Bayfront\ArrayHelpers\Arr;
279
-
280
268
$array = [
281
269
'name' => [
282
270
'first_name' => 'John',
@@ -307,8 +295,6 @@ Returns the original array except given key(s).
307
295
** Example:**
308
296
309
297
```
310
- use Bayfront\ArrayHelpers\Arr;
311
-
312
298
$array = [
313
299
'user_id' => 110,
314
300
'username' => 'John',
@@ -338,8 +324,6 @@ Returns only desired key(s) from an array.
338
324
** Example:**
339
325
340
326
```
341
- use Bayfront\ArrayHelpers\Arr;
342
-
343
327
$array = [
344
328
'user_id' => 110,
345
329
'username' => 'John',
@@ -369,8 +353,6 @@ Returns array of missing keys from the original array, or an empty array if none
369
353
** Example:**
370
354
371
355
```
372
- use Bayfront\ArrayHelpers\Arr;
373
-
374
356
$array = [
375
357
'user_id' => 110,
376
358
'username' => 'John',
@@ -403,8 +385,6 @@ Checks if keys are missing from the original array.
403
385
** Example:**
404
386
405
387
```
406
- use Bayfront\ArrayHelpers\Arr;
407
-
408
388
$array = [
409
389
'user_id' => 110,
410
390
'username' => 'John',
@@ -440,8 +420,6 @@ Sort a multidimensional array by a given key in ascending (optionally, descendin
440
420
** Example:**
441
421
442
422
```
443
- use Bayfront\ArrayHelpers\Arr;
444
-
445
423
$clients = [
446
424
[
447
425
'first_name' => 'John',
@@ -458,6 +436,51 @@ $sorted = Arr::multisort($clients, 'first_name');
458
436
459
437
<hr />
460
438
439
+ ### numericMultisort
440
+
441
+ ** Description:**
442
+
443
+ Sort a numerically indexed array of multidimensional arrays by a given key in ascending (optionally, descending) order.
444
+
445
+ ** Parameters:**
446
+
447
+ - ` $array ` (array): Numerically indexed array of multidimensional arrays
448
+ - ` $key ` (string): Key name to sort by in dot notation
449
+ - ` $descending = false ` (bool): Sort descending
450
+
451
+ ** Returns:**
452
+
453
+ - (array)
454
+
455
+ ** Example:**
456
+
457
+ ```
458
+ $contacts = [
459
+ [
460
+ 'id' => 1,
461
+ 'contact' => [
462
+ 'name' => 'James'
463
+ ]
464
+ ],
465
+ [
466
+ 'id' => 2,
467
+ 'contact' => [
468
+ 'name' => 'Bob'
469
+ ]
470
+ ],
471
+ [
472
+ 'id' => 3,
473
+ 'contact' => [
474
+ 'name' => 'Carl'
475
+ ]
476
+ ]
477
+ ];
478
+
479
+ $sorted = Arr::numericMultisort($contacts, 'contact.name');
480
+ ```
481
+
482
+ <hr />
483
+
461
484
### renameKeys
462
485
463
486
** Description:**
@@ -476,8 +499,6 @@ Rename array keys while preserving their order.
476
499
** Example:**
477
500
478
501
```
479
- use Bayfront\ArrayHelpers\Arr;
480
-
481
502
$user = [
482
503
'UserID' => 5,
483
504
'UserEmail' => '[email protected] ',
@@ -513,8 +534,6 @@ Keys from the `$order` array which do not exist in the original array will be ig
513
534
** Example:**
514
535
515
536
```
516
- use Bayfront\ArrayHelpers\Arr;
517
-
518
537
$address = [
519
538
'street' => '123 Main St.',
520
539
'state' => 'IL',
@@ -564,8 +583,6 @@ Convert array into a query string.
564
583
** Example:**
565
584
566
585
```
567
- use Bayfront\ArrayHelpers\Arr;
568
-
569
586
$array = [
570
587
'first_name' => 'Jane',
571
588
'last_name' => 'Doe'
@@ -675,4 +692,56 @@ if (Arr::hasAllValues($array['name'], [
675
692
])) {
676
693
// Do something
677
694
}
695
+ ```
696
+
697
+ <hr />
698
+
699
+ ### ensureHas
700
+
701
+ ** Description:**
702
+
703
+ Ensure a numerically indexed array of arrays has a given item based on a unique key.
704
+
705
+ ** Parameters:**
706
+
707
+ - ` $array ` (array)
708
+ - ` $item ` (array): Item to exist
709
+ - ` $unique_key ` (string): Unique array key
710
+
711
+ ** Returns:**
712
+
713
+ - (bool)
714
+
715
+ ** Example:**
716
+
717
+ ```
718
+ $contacts = [
719
+ [
720
+ 'id' => 1,
721
+ 'contact' => [
722
+ 'name' => 'James'
723
+ ]
724
+ ],
725
+ [
726
+ 'id' => 2,
727
+ 'contact' => [
728
+ 'name' => 'Bob'
729
+ ]
730
+ ],
731
+ [
732
+ 'id' => 3,
733
+ 'contact' => [
734
+ 'name' => 'Carl'
735
+ ]
736
+ ]
737
+ ];
738
+
739
+ $item = [
740
+ 'id' => 2,
741
+ 'contact' => [
742
+ 'name' => 'Bob'
743
+ ]
744
+ ];
745
+
746
+ $contacts = Arr::ensureHas($contacts, $item, 'id');
678
747
```
0 commit comments