Skip to content

Commit 170093b

Browse files
committed
Added numericMultisort and ensureHas methods
1 parent edb9133 commit 170093b

File tree

3 files changed

+181
-65
lines changed

3 files changed

+181
-65
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `Fixed` for any bug fixes.
1313
- `Security` in case of vulnerabilities
1414

15+
## [2.1.0] - 2025.07.10
16+
17+
### Added
18+
19+
- Added `numericMultisort` and `ensureHas` methods
20+
21+
22+
### Changed
23+
24+
- Updated documentation
25+
1526
## [2.0.2] - 2024.12.23
1627

1728
### Added

README.md

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ composer require bayfrontmedia/php-array-helpers
4343
- [missing](#missing)
4444
- [isMissing](#ismissing)
4545
- [multisort](#multisort)
46+
- [numericMultisort](#numericmultisort)
4647
- [renameKeys](#renamekeys)
4748
- [order](#order)
4849
- [query](#query)
4950
- [getAnyValues](#getanyvalues)
5051
- [hasAnyValues](#hasanyvalues)
5152
- [hasAllValues](#hasallvalues)
53+
- [ensureHas](#ensurehas)
5254

5355
<hr />
5456

@@ -72,8 +74,6 @@ The key values will never be an array, even if empty. Empty arrays will be dropp
7274
**Example:**
7375

7476
```
75-
use Bayfront\ArrayHelpers\Arr;
76-
7777
$array = [
7878
'name' => [
7979
'first_name' => 'John',
@@ -106,8 +106,6 @@ Converts array in "dot" notation to a standard multidimensional array.
106106
**Example:**
107107

108108
```
109-
use Bayfront\ArrayHelpers\Arr;
110-
111109
$array = [
112110
'name.first_name' => 'John',
113111
'name.last_name' => 'Doe'
@@ -137,8 +135,6 @@ Set an array item to a given value using "dot" notation.
137135
**Example:**
138136

139137
```
140-
use Bayfront\ArrayHelpers\Arr;
141-
142138
$array = [
143139
'name' => [
144140
'first_name' => 'John',
@@ -169,8 +165,6 @@ Checks if array key exists and not null using "dot" notation.
169165
**Example:**
170166

171167
```
172-
use Bayfront\ArrayHelpers\Arr;
173-
174168
$array = [
175169
'name' => [
176170
'first_name' => 'John',
@@ -204,8 +198,6 @@ Get an item from an array using "dot" notation, returning an optional default va
204198
**Example:**
205199

206200
```
207-
use Bayfront\ArrayHelpers\Arr;
208-
209201
$array = [
210202
'name' => [
211203
'first_name' => 'John',
@@ -237,8 +229,6 @@ Returns an array of values for a given key from an array using "dot" notation.
237229
**Example:**
238230

239231
```
240-
use Bayfront\ArrayHelpers\Arr;
241-
242232
$array = [
243233
[
244234
'user_id' => 110,
@@ -275,8 +265,6 @@ Remove a single key, or an array of keys from a given array using "dot" notation
275265
**Example:**
276266

277267
```
278-
use Bayfront\ArrayHelpers\Arr;
279-
280268
$array = [
281269
'name' => [
282270
'first_name' => 'John',
@@ -307,8 +295,6 @@ Returns the original array except given key(s).
307295
**Example:**
308296

309297
```
310-
use Bayfront\ArrayHelpers\Arr;
311-
312298
$array = [
313299
'user_id' => 110,
314300
'username' => 'John',
@@ -338,8 +324,6 @@ Returns only desired key(s) from an array.
338324
**Example:**
339325

340326
```
341-
use Bayfront\ArrayHelpers\Arr;
342-
343327
$array = [
344328
'user_id' => 110,
345329
'username' => 'John',
@@ -369,8 +353,6 @@ Returns array of missing keys from the original array, or an empty array if none
369353
**Example:**
370354

371355
```
372-
use Bayfront\ArrayHelpers\Arr;
373-
374356
$array = [
375357
'user_id' => 110,
376358
'username' => 'John',
@@ -403,8 +385,6 @@ Checks if keys are missing from the original array.
403385
**Example:**
404386

405387
```
406-
use Bayfront\ArrayHelpers\Arr;
407-
408388
$array = [
409389
'user_id' => 110,
410390
'username' => 'John',
@@ -440,8 +420,6 @@ Sort a multidimensional array by a given key in ascending (optionally, descendin
440420
**Example:**
441421

442422
```
443-
use Bayfront\ArrayHelpers\Arr;
444-
445423
$clients = [
446424
[
447425
'first_name' => 'John',
@@ -458,6 +436,51 @@ $sorted = Arr::multisort($clients, 'first_name');
458436

459437
<hr />
460438

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+
461484
### renameKeys
462485

463486
**Description:**
@@ -476,8 +499,6 @@ Rename array keys while preserving their order.
476499
**Example:**
477500

478501
```
479-
use Bayfront\ArrayHelpers\Arr;
480-
481502
$user = [
482503
'UserID' => 5,
483504
'UserEmail' => '[email protected]',
@@ -513,8 +534,6 @@ Keys from the `$order` array which do not exist in the original array will be ig
513534
**Example:**
514535

515536
```
516-
use Bayfront\ArrayHelpers\Arr;
517-
518537
$address = [
519538
'street' => '123 Main St.',
520539
'state' => 'IL',
@@ -564,8 +583,6 @@ Convert array into a query string.
564583
**Example:**
565584

566585
```
567-
use Bayfront\ArrayHelpers\Arr;
568-
569586
$array = [
570587
'first_name' => 'Jane',
571588
'last_name' => 'Doe'
@@ -675,4 +692,56 @@ if (Arr::hasAllValues($array['name'], [
675692
])) {
676693
// Do something
677694
}
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');
678747
```

0 commit comments

Comments
 (0)