@@ -26,18 +26,36 @@ $multiArray = [
26
26
'hi' => [ 'de' => 'Hallo', 'es' => 'Hola' ]
27
27
];
28
28
29
- $array = Flatten::flatten($multiArray);
30
-
31
- /* print_r($array) gives:
32
-
33
- Array
34
- (
35
- [say] => what
36
- [hi.de] => Hallo
37
- [hi.es] => Hola
38
- )
29
+ /*
30
+ Flatten::__construct(
31
+ string $separator = '.',
32
+ string $prefix = '',
33
+ int $flags = 0
34
+ )
35
+ */
36
+ $flatten = new Flatten();
37
+
38
+ // Flatten::flattenToArray is provided for convinience. It internally
39
+ // calls Flatten::flatten and converts it's output, which is a 1-dimensional
40
+ // iterator, into a 1-dimensional array.
41
+ $flattened = $flatten->flattenToArray($multiArray);
42
+
43
+ // Flatten::unflattenToArray is provided for convinience. It internally
44
+ // calls Flatten::unflatten and converts it's output, which is a recursive
45
+ // generator structure, into a multi-dimensional array.
46
+ $unflattened = $flatten->unflattenToArray($flattened);
47
+
48
+ /*
49
+ assert($flattened == [
50
+ 'say' => what
51
+ 'hi.de' => Hallo
52
+ 'hi.es' => Hola
53
+ ]);
54
+
55
+ assert($unflattened == $multiArray);
39
56
*/
40
57
```
58
+
41
59
** Example 2**
42
60
43
61
Custom Separator and initial prefix
@@ -49,25 +67,31 @@ $allowAccess = [
49
67
'var' => [ 'log' => ['nginx' => true, 'apt' => false], 'www' => true ],
50
68
];
51
69
52
- $allowAccess = Flatten::flatten($allowAccess, '/', '/');
70
+ $flatten = new Flatten(
71
+ '/', // separator
72
+ '/' // prefix
73
+ );
74
+
75
+ $flattened = $flatten->flattenToArray($allowAccess);
53
76
54
- /* var_dump($array) gives:
77
+ $unflattened = $flatten->unflattenToArray($flattened);
55
78
56
- array(4) {
57
- '/root' =>
58
- bool(false)
59
- '/var/log/nginx' =>
60
- bool(true)
61
- '/var/log/apt' =>
62
- bool(false)
63
- '/var/www' =>
64
- bool(true)
65
- }
79
+ /*
80
+ assert($flatten == [
81
+ '/root' => false,
82
+ '/var/log/nginx' => true,
83
+ '/var/log/apt' => false,
84
+ '/var/www' => true
85
+ ]);
86
+
87
+ assert($unflattened == $allowAccess);
66
88
*/
67
89
```
90
+
68
91
** Example 3**
69
92
70
93
Notice that the prefix will not be separated in FQkeys. If it should be separated, separator must be appeneded to the prefix string.
94
+
71
95
``` php
72
96
use Sarhan\Flatten\Flatten;
73
97
@@ -76,20 +100,23 @@ $api = [
76
100
'tag' => [ 'soccer' => 7124, 'tennis' => [ 'singles' => 9833, 'doubles' => 27127 ] ],
77
101
];
78
102
79
- $uris = Flatten::flatten($api, '/', 'https://api.dummyhost.domain/');
103
+ $flatten = new Flatten( '/', 'https://api.dummyhost.domain/');
80
104
81
- /* print_r($uris) gives:
105
+ $flattened = $flatten->flattenToArray($api);
82
106
83
- Array
84
- (
85
- [https://api.dummyhost.domain/category/health] => 321
86
- [https://api.dummyhost.domain/category/sport] => 769
87
- [https://api.dummyhost.domain/category/fashion] => 888
88
- [https://api.dummyhost.domain/tag/soccer] => 7124
89
- [https://api.dummyhost.domain/tag/tennis/singles] => 9833
90
- [https://api.dummyhost.domain/tag/tennis/doubles] => 27127
91
- )
107
+ $unflattened = $flatten->unflattenToArray($flattened);
92
108
109
+ /*
110
+ assert($flattened == [
111
+ 'https://api.dummyhost.domain/category/health' => 321,
112
+ 'https://api.dummyhost.domain/category/sport' => 769,
113
+ 'https://api.dummyhost.domain/category/fashion' => 888,
114
+ 'https://api.dummyhost.domain/tag/soccer' => 7124,
115
+ 'https://api.dummyhost.domain/tag/tennis/singles' => 9833,
116
+ 'https://api.dummyhost.domain/tag/tennis/doubles' => 27127
117
+ ]);
118
+
119
+ assert($unflattened == $api);
93
120
*/
94
121
```
95
122
@@ -107,19 +134,24 @@ $nutrition = [
107
134
'fruits' => [ 'oranges', 'apple', 'banana' ],
108
135
'veggies' => ['lettuce', 'broccoli'],
109
136
];
110
-
111
- $nutrition = Flatten::flatten($nutrition, '-');
112
-
113
- /* print_r($nutrition):
114
- Array
115
- (
116
- [0] => nutrition
117
- [fruits-0] => oranges
118
- [fruits-1] => apple
119
- [fruits-2] => banana
120
- [veggies-0] => lettuce
121
- [veggies-1] => broccoli
122
- )
137
+
138
+ $flatten = new Flatten('-');
139
+
140
+ $flattened = $flatten->flattenToArray($nutrition);
141
+
142
+ $unflattened = $flatten->unflattenToArray($flattened);
143
+
144
+ /*
145
+ assert($flattened == [
146
+ '0' => 'nutrition',
147
+ 'fruits-0' => 'oranges',
148
+ 'fruits-1' => 'apple',
149
+ 'fruits-2' => 'banana',
150
+ 'veggies-0' => 'lettuce',
151
+ 'veggies-1' => 'broccoli'
152
+ ]);
153
+
154
+ assert($unflattened == $nutrition);
123
155
*/
124
156
```
125
157
@@ -142,36 +174,36 @@ $examples = [
142
174
'values' => [3 => 'hello world', 5 => 'what is your name?']
143
175
];
144
176
145
- $flattened = Flatten::flatten($examples, '.', 'examples.', Flatten::FLAG_NUMERIC_NOT_FLATTENED);
146
-
147
- /* print_r($flattened):
148
- Array
149
- (
150
- [examples.templates] => Array
151
- (
152
- [0] => Array
153
- (
154
- [lang] => js
155
- [template] => console.log('%s');
156
- )
157
-
158
- [1] => Array
159
- (
160
- [lang] => php
161
- [template] => echo "%s";
162
- )
163
-
164
- )
165
-
166
- [examples.values] => Array
167
- (
168
- [3] => hello world
169
- [5] => what is your name?
170
- )
177
+ $flatten = new Flatten(
178
+ '.',
179
+ 'examples.',
180
+ Flatten::FLAG_NUMERIC_NOT_FLATTENED
181
+ );
182
+
183
+ $flattened = $flatten->flattenToArray($examples);
184
+
185
+ $unflattened = $flatten->unflattenToArray($flattened);
186
+
187
+ /*
188
+ assert($flattened == [
189
+ 'examples.templates' => [
190
+ [
191
+ 'lang' => 'js',
192
+ 'template' => 'console.log(\'%s\')';
193
+ ],
194
+ [
195
+ 'lang' => 'php',
196
+ 'template' => 'echo "%s"'
197
+ ]
198
+ ],
199
+ 'examples.values' => [
200
+ 3 => 'hello world',
201
+ 5 => 'what is your name?'
202
+ ]
203
+ ]);
171
204
172
- )
205
+ assert($unflattened == $examples);
173
206
*/
174
-
175
207
```
176
208
Top level numeric (integer) keys will also be returned into an array assigned to the passed prefix.
177
209
@@ -187,31 +219,23 @@ $seats = [
187
219
'_blocked' => ['B2']
188
220
];
189
221
190
- $flattened = Flatten::flatten($seats, '_', 'seats', Flatten::FLAG_NUMERIC_NOT_FLATTENED);
191
-
192
- /* print_r($flattened)
193
-
194
- Array
195
- (
196
- [seats] => Array
197
- (
198
- [0] => A1
199
- [1] => A2
200
- [2] => B1
201
- [3] => B2
202
- )
203
-
204
- [seats_reserved] => Array
205
- (
206
- [0] => A1
207
- [1] => B1
208
- )
209
-
210
- [seats_blocked] => Array
211
- (
212
- [0] => B2
213
- )
214
- )
222
+ $flatten = new Flatten(
223
+ '_',
224
+ 'seats',
225
+ Flatten::FLAG_NUMERIC_NOT_FLATTENED
226
+ );
227
+
228
+ $flattened = $flatten->flattenToArray($seats);
229
+
230
+ $unflattened = $flatten->unflattenToArray($flattened);
231
+
232
+ /*
233
+ assert($flattened == [
234
+ 'seats' => ['A1', 'A2', 'B1', 'B2'],
235
+ 'seats_reserved' => ['A1', 'B1'],
236
+ 'seats_blocked' => ['B2']
237
+ ]);
215
238
239
+ assert($unflattened == $seats);
216
240
*/
217
241
```
0 commit comments