Skip to content

Commit d2efc0c

Browse files
Merge pull request #83 from Hi-Folks/feat/nested-get
get() supports nested arrays
2 parents 1491f74 + 113bdb6 commit d2efc0c

5 files changed

Lines changed: 121 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.0.2 - 2024-05-24
4+
- The Arr `get()` method supports 'dot' (or custom) notation for nested arrays for example $arr->get('first-level.second-level.third-level')
5+
- The Arr `get()` method supports the default value
6+
37
## 1.0.1 - 2024-05-23
48
Fine-tuning release: removing version from composer.json and fixing changelog
59

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,43 @@ The Arr class provides some methods:
6363
- set(): ability to set an element to the array with a specific key
6464
- unset(): ability to unset an element by the key
6565

66+
### The `get()` method
67+
The `get()` method supports keys with the dot (or custom) notation for retrieving values from nested arrays.
68+
For example:
69+
70+
```php
71+
$fruits = Arr::make([
72+
'green' => [
73+
'kiwi' => '🥝',
74+
'mango' => '🥭'
75+
],
76+
'red' => [
77+
'strawberry' => '🍓',
78+
'apple' => '🍎'
79+
],
80+
'yellow' => [
81+
'lemon' => '🍋',
82+
'banana' => '🍌',
83+
]
84+
]);
85+
$fruits->get('red'); // 🍓,🍎
86+
$fruits->get('red.strawberry'); // 🍓
87+
```
88+
89+
You can customize the notation with a different character:
90+
91+
```php
92+
$fruits->get('red#strawberry', charNestedKey: '#'); // 🍓
93+
```
94+
You can define a default value in the case the key doesn't exist:
95+
96+
```php
97+
$fruits->get('red#somestrangefruit',
98+
'🫠', '#'); // 🫠
99+
```
100+
101+
102+
66103
## Table class
67104
Table class allows you to manage bi dimensional array, something like:
68105
```

examples/cheatsheet.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
// Extract keys with keys()
162162
$fruits = Arr::make([
163163
'kiwi' => '🥝',
164-
'fragola' => '🍓',
164+
'strawberry' => '🍓',
165165
'lemon' => '🍋',
166166
'mango' => '🥭',
167167
'apple' => '🍎',
@@ -192,7 +192,7 @@
192192
// Extract entries with entries()
193193
$fruits = Arr::make([
194194
'kiwi' => '🥝',
195-
'fragola' => '🍓',
195+
'strawberry' => '🍓',
196196
'lemon' => '🍋',
197197
'mango' => '🥭',
198198
'apple' => '🍎',
@@ -252,6 +252,22 @@
252252
print_result($arr->arr());
253253
// ['mango' => '🥭', 'apple' => '🍎']
254254

255+
$fruits = Arr::make([
256+
'green' => [
257+
'kiwi' => '🥝',
258+
'mango' => '🥭'
259+
],
260+
'red' => [
261+
'strawberry' => '🍓',
262+
'apple' => '🍎'
263+
],
264+
'yellow' => [
265+
'lemon' => '🍋',
266+
'banana' => '🍌',
267+
]
268+
]);
269+
print_result($fruits->get('red'));
270+
print_result($fruits->get('red.strawberry'));
255271

256272

257273

src/Arr.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,25 @@ public function arr(): array
108108

109109
/**
110110
* Get the element with $index
111-
*/
112-
public function get(mixed $index): mixed
113-
{
114-
return $this->arr[$index] ?? null;
111+
* @param non-empty-string $charNestedKey
112+
*/
113+
public function get(mixed $index, mixed $defaultValue = null, string $charNestedKey = "."): mixed
114+
{
115+
if (is_string($index)) {
116+
$indexString = strval($index);
117+
if (str_contains($indexString, $charNestedKey)) {
118+
$nestedValue = $this->arr;
119+
foreach (explode($charNestedKey, $indexString) as $nestedKey) {
120+
if (is_array($nestedValue) && array_key_exists($nestedKey, $nestedValue)) {
121+
$nestedValue = $nestedValue[$nestedKey];
122+
} else {
123+
return $defaultValue;
124+
}
125+
}
126+
return $nestedValue;
127+
}
128+
}
129+
return $this->arr[$index] ?? $defaultValue;
115130
}
116131

117132
/**

tests/ArrGetTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use HiFolks\DataType\Arr;
4+
5+
it('Basic get', function (): void {
6+
$arr = Arr::make(['A','B','C']);
7+
expect($arr->get(1))->toBe('B');
8+
expect($arr->get(4))->toBeNull();
9+
expect($arr->get(4, 'AAAA'))->toBe("AAAA");
10+
});
11+
12+
it('Basic nested get', function (): void {
13+
$arr = Arr::make([
14+
'A' => 'First',
15+
'B' => ['some', 'thing'],
16+
'C' => [ 'nested-item-1' => 10, 'nested-item-2' => 20],
17+
'D' => []
18+
]);
19+
expect($arr->get('A'))->toBe('First');
20+
expect($arr->get('B'))->toBeArray();
21+
expect($arr->get('B.0'))->toBe('some');
22+
expect($arr->get('B.1'))->toBe('thing');
23+
expect($arr->get('B.2'))->toBeNull();
24+
expect($arr->get('B.2', 1234))->toBe(1234);
25+
expect($arr->get('B#0', charNestedKey: '#'))->toBe('some');
26+
expect($arr->get('B#1', charNestedKey: '#'))->toBe('thing');
27+
expect($arr->get('B#2', charNestedKey: '#'))->toBeNull();
28+
expect($arr->get('B#2', 1234, '#'))->toBe(1234);
29+
30+
expect($arr->get('C.0'))->toBeNull();
31+
expect($arr->get('C.nested-item-1'))->toBe(10);
32+
expect($arr->get('C.nested-item-2'))->toBe(20);
33+
expect($arr->get('C.nested-item-2.other'))->toBeNull();
34+
expect($arr->get('C.nested-item-2.other', 'zzz'))->toBe('zzz');
35+
expect($arr->get('C#nested-item-2#other', 'zzz', '#'))->toBe('zzz');
36+
expect($arr->get('C#nested-item-2#other', 'zzz'))->toBe('zzz');
37+
expect($arr->get('C#nested-item-2', 'zzz'))->toBe('zzz');
38+
expect($arr->get('C#nested-item-4', 'zzz'))->toBe('zzz');
39+
expect($arr->get('D#nested-item-4', 'zzz'))->toBe('zzz');
40+
expect($arr->get('D.0'))->toBeNull();
41+
expect($arr->get('D', '0'))->toBeArray()->toHaveLength(0);
42+
43+
});

0 commit comments

Comments
 (0)