pack

Definition

1
Arr::pack(array $array): array
Copied!

Description

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

Examples

Let's use result array from example below.
1
$array = [
2
'key1.key2.key3.foo' => 'test',
3
'key1.key2.key3.bar' => 'test2',
4
'key1.abc.0' => 'test3',
5
'xyz' => 'test4',
6
'0' => 'test5',
7
];
8
9
Arr::pack($array) ->
10
[
11
'key1' => [
12
'key2' => [
13
'key3' => [
14
'foo' => 'test',
15
'bar' => 'test2',
16
]
17
]
18
'abc' => ['test3'],
19
],
20
'xyz' => 'test4',
21
'test5'
22
]
23
24
// Unpack is inverse operation to pack
25
$array2 = [
26
'test',
27
[
28
'foo' => ['bar'],
29
'a' => [
30
'b' => 1
31
]
32
]
33
];
34
35
Arr::unpack(Arr::pack($array2)) === $array2 -> true
Copied!
Last modified 2yr ago
Copy link