unpack

Definition

1
Arr::unpack(array $array, int $mode = Arr::UNPACK_ALL): array
Copied!

Description

Converts multidimensional array to map of keys concatenated by dot and corresponding values.
Inverse of pack.

Modes

Mode
Description
Arr::UNPACK_ALL
Every array will be unpacked
Arr::UNPACK_PRESERVE_LIST_ARRAY
Preserve arrays with highest nesting level (if they are not associative) as element values instead of unpacking them
Arr::UNPACK_PRESERVE_ASSOC_ARRAY
Preserve arrays with highest nesting level (if they are associative) as element values instead of unpacking them
Arr::UNPACK_PRESERVE_ARRAY
Preserve all arrays with highest nesting level as element values instead of unpacking them

Examples

1
$array = [
2
'key1' => [
3
'key2' => [
4
'key3' => [
5
'foo' => 'test',
6
'bar' => 'test2',
7
]
8
]
9
'abc' => ['test3'],
10
],
11
'xyz' => 'test4',
12
'test5'
13
];
14
15
// Equal to Arr::unpack($array, Arr::UNPACK_ALL)
16
Arr::unpack($array) ->
17
[
18
'key1.key2.key3.foo' => 'test',
19
'key1.key2.key3.bar' => 'test2',
20
'key1.abc.0' => 'test3',
21
'xyz' => 'test4',
22
'0' => 'test5',
23
]
24
25
Arr::unpack($array, Arr::UNPACK_PRESERVE_LIST_ARRAY) ->
26
[
27
'key1.key2.key3.foo' => 'test',
28
'key1.key2.key3.bar' => 'test2',
29
// Preserve list array as value
30
'key1.abc' => ['test3'],
31
'xyz' => 'test4',
32
'0' => 'test5',
33
]
34
35
Arr::unpack($array, Arr::UNPACK_PRESERVE_ASSOC_ARRAY) ->
36
[
37
// Preserve assoc array as value
38
'key1.key2.key3' => [
39
'foo' => 'test',
40
'bar' => 'test2',
41
],
42
'key1.abc.0' => 'test3',
43
'xyz' => 'test4',
44
'0' => 'test5',
45
]
46
47
Arr::unpack($array, Arr::UNPACK_PRESERVE_ARRAY) ->
48
[
49
// Preserve assoc array as value
50
'key1.key2.key3' => [
51
'foo' => 'test',
52
'bar' => 'test2',
53
],
54
// Preserve list array as value
55
'key1.abc' => ['test3'],
56
'xyz' => 'test4',
57
'0' => 'test5',
58
]
Copied!
Last modified 2yr ago
Copy link