Skip to content

Commit 05e3810

Browse files
minworkgitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent bb06a81 commit 05e3810

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

documentation-1/common.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,60 @@ Arr::setNestedElement($array, 'foo.[].foo', 'bar') ->
111111
Arr::setNestedElement([], '[].[].[]', 'test') -> [ [ [ 'test' ] ] ]
112112
```
113113

114+
## pack
115+
116+
#### Definition
117+
118+
`pack(array $array): array`
119+
120+
#### Description
121+
122+
Converts map of keys concatenated by dot and corresponding values to multidimensional array.
123+
124+
Inverse of [`unpack`](common.md#unpack).
125+
126+
#### Examples
127+
128+
Let's use result array from [example below](common.md#examples-5).
129+
130+
```php
131+
$array = [
132+
'key1.key2.key3.foo' => 'test',
133+
'key1.key2.key3.bar' => 'test2',
134+
'key1.abc.0' => 'test3',
135+
'xyz' => 'test4',
136+
'0' => 'test5',
137+
];
138+
139+
Arr::pack($array) ->
140+
[
141+
'key1' => [
142+
'key2' => [
143+
'key3' => [
144+
'foo' => 'test',
145+
'bar' => 'test2',
146+
]
147+
]
148+
'abc' => ['test3'],
149+
],
150+
'xyz' => 'test4',
151+
'test5'
152+
]
153+
154+
// Unpack is inverse operation to pack
155+
$array2 = [
156+
'test',
157+
[
158+
'foo' => ['bar'],
159+
'a' => [
160+
'b' => 1
161+
]
162+
]
163+
];
164+
165+
Arr::unpack(Arr::pack($array2)) === $array2 -> true
166+
```
167+
114168
## unpack
115169

116170
#### Definition
@@ -121,6 +175,17 @@ Arr::setNestedElement([], '[].[].[]', 'test') -> [ [ [ 'test' ] ] ]
121175

122176
Converts multidimensional array to map of keys concatenated by dot and corresponding values.
123177

178+
Inverse of [`pack`](common.md#pack).
179+
180+
#### Modes
181+
182+
| Mode | Description |
183+
| :--- | :--- |
184+
| `Arr::UNPACK_ALL` | Every array will be unpacked |
185+
| `Arr::UNPACK_PRESERVE_LIST_ARRAY` | Preserve arrays with highest nesting level \(if they are not associative\) as element values instead of unpacking them |
186+
| `Arr::UNPACK_PRESERVE_ASSOC_ARRAY` | Preserve arrays with highest nesting level \(if they are associative\) as element values instead of unpacking them |
187+
| `Arr::UNPACK_PRESERVE_ARRAY` | Preserve all arrays with highest nesting level as element values instead of unpacking them |
188+
124189
#### Examples
125190

126191
```php
@@ -132,17 +197,53 @@ $array = [
132197
'bar' => 'test2',
133198
]
134199
]
135-
'abc' => 'test3',
200+
'abc' => ['test3'],
136201
],
137202
'xyz' => 'test4',
138203
'test5'
139204
];
140205

206+
// Equal to Arr::unpack($array, Arr::UNPACK_ALL)
141207
Arr::unpack($array) ->
142208
[
143209
'key1.key2.key3.foo' => 'test',
144210
'key1.key2.key3.bar' => 'test2',
145-
'key1.abc' => 'test3',
211+
'key1.abc.0' => 'test3',
212+
'xyz' => 'test4',
213+
'0' => 'test5',
214+
]
215+
216+
Arr::unpack($array, Arr::UNPACK_PRESERVE_LIST_ARRAY) ->
217+
[
218+
'key1.key2.key3.foo' => 'test',
219+
'key1.key2.key3.bar' => 'test2',
220+
// Preserve list array as value
221+
'key1.abc' => ['test3'],
222+
'xyz' => 'test4',
223+
'0' => 'test5',
224+
]
225+
226+
Arr::unpack($array, Arr::UNPACK_PRESERVE_ASSOC_ARRAY) ->
227+
[
228+
// Preserve assoc array as value
229+
'key1.key2.key3' => [
230+
'foo' => 'test',
231+
'bar' => 'test2',
232+
],
233+
'key1.abc.0' => 'test3',
234+
'xyz' => 'test4',
235+
'0' => 'test5',
236+
]
237+
238+
Arr::unpack($array, Arr::UNPACK_PRESERVE_ARRAY) ->
239+
[
240+
// Preserve assoc array as value
241+
'key1.key2.key3' => [
242+
'foo' => 'test',
243+
'bar' => 'test2',
244+
],
245+
// Preserve list array as value
246+
'key1.abc' => ['test3'],
146247
'xyz' => 'test4',
147248
'0' => 'test5',
148249
]

0 commit comments

Comments
 (0)