set → setNestedElement

Definition

1
Arr::setNestedElement(array|ArrayAccess $array, mixed $keys, mixed $value): array|ArrayAccess
Copied!

Aliases

1
set(array|ArrayAccess $array, $keys, $value) -> setNestedElement(array|ArrayAccess $array, $keys, $value)
Copied!

Description

Set array element specified by keys to the desired value (create missing keys if necessary).
$keys argument is parsed using getKeysArray method

Examples

1
$array = ['key1' => ['key2' => ['key3' => ['test']]]];
2
3
Arr::setNestedElement([], 'key1.key2.key3', ['test']) -> $array
4
5
$array = Arr::setNestedElement($array, 'key1.key2.key4', 'test2');
6
$array['key1']['key2']['key4'] -> 'test2'
7
8
// Create nested array element using automatic index
9
Arr::setNestedElement($array, 'foo.[].foo', 'bar') ->
10
[
11
'foo' => [
12
[
13
'foo' => 'bar',
14
],
15
],
16
]
17
18
Arr::setNestedElement([], '[].[].[]', 'test') -> [ [ [ 'test' ] ] ]
Copied!
Last modified 1yr ago
Copy link