@@ -26,18 +26,24 @@ $multiArray = [
2626 'hi' => [ 'de' => 'Hallo', 'es' => 'Hola' ]
2727];
2828
29- $array = Flatten::flatten($multiArray );
29+ $flatten = new Flatten( );
3030
31- /* print_r($array) gives:
31+ $flattened = $flatten->flatten($multiArray);
3232
33- Array
34- (
35- [say] => what
36- [hi.de] => Hallo
37- [hi.es] => Hola
38- )
33+ $unflattened = $flatten->unflatten($flattened);
34+
35+ /*
36+ assert($flattened == [
37+ 'say' => what
38+ 'hi.de' => Hallo
39+ 'hi.es' => Hola
40+ ]);
41+
42+ assert($unflattened == $multiArray);
3943*/
44+
4045```
46+
4147** Example 2**
4248
4349Custom Separator and initial prefix
@@ -49,25 +55,31 @@ $allowAccess = [
4955 'var' => [ 'log' => ['nginx' => true, 'apt' => false], 'www' => true ],
5056];
5157
52- $allowAccess = Flatten::flatten($allowAccess, '/', '/');
58+ $flatten = new Flatten(
59+ '/', // separator
60+ '/' // prefix
61+ );
62+
63+ $flattened = $flatten->flatten($allowAccess);
5364
54- /* var_dump($array) gives:
65+ $unflattened = $flatten->unflattenToArray($flattened);
5566
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- }
67+ /*
68+ assert($flatten == [
69+ '/root' => false,
70+ '/var/log/nginx' => true,
71+ '/var/log/apt' => false,
72+ '/var/www' => true
73+ ]);
74+
75+ assert($unflattened == $allowAccess);
6676*/
6777```
78+
6879** Example 3**
6980
7081Notice that the prefix will not be separated in FQkeys. If it should be separated, separator must be appeneded to the prefix string.
82+
7183``` php
7284use Sarhan\Flatten\Flatten;
7385
@@ -76,20 +88,23 @@ $api = [
7688 'tag' => [ 'soccer' => 7124, 'tennis' => [ 'singles' => 9833, 'doubles' => 27127 ] ],
7789];
7890
79- $uris = Flatten::flatten($api, '/', 'https://api.dummyhost.domain/');
91+ $flatten = new Flatten('/', 'https://api.dummyhost.domain/');
92+
93+ $flattened = $flatten->flatten($api);
8094
81- /* print_r($uris) gives:
95+ $unflattened = $flatten->unflattenToArray($flattened);
8296
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- )
97+ /*
98+ assert($flattened == [
99+ ' https://api.dummyhost.domain/category/health' => 321,
100+ ' https://api.dummyhost.domain/category/sport' => 769,
101+ ' https://api.dummyhost.domain/category/fashion' => 888,
102+ ' https://api.dummyhost.domain/tag/soccer' => 7124,
103+ ' https://api.dummyhost.domain/tag/tennis/singles' => 9833,
104+ ' https://api.dummyhost.domain/tag/tennis/doubles' => 27127
105+ ]);
92106
107+ assert($unflattened == $api);
93108*/
94109```
95110
@@ -107,19 +122,24 @@ $nutrition = [
107122 'fruits' => [ 'oranges', 'apple', 'banana' ],
108123 'veggies' => ['lettuce', 'broccoli'],
109124];
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- )
125+
126+ $flatten = new Flatten('-');
127+
128+ $flattened = $flatten->flatten($nutrition);
129+
130+ $unflattened = $flatten->unflattenToArray($flattened);
131+
132+ /*
133+ assert($flattened == [
134+ '0' => 'nutrition',
135+ 'fruits-0' => 'oranges',
136+ 'fruits-1' => 'apple',
137+ 'fruits-2' => 'banana',
138+ 'veggies-0' => 'lettuce',
139+ 'veggies-1' => 'broccoli'
140+ ]);
141+
142+ assert($unflattened == $nutrition);
123143*/
124144```
125145
@@ -142,34 +162,35 @@ $examples = [
142162 'values' => [3 => 'hello world', 5 => 'what is your name?']
143163];
144164
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- )
171-
172- )
165+ $flatten = new Flatten(
166+ '.',
167+ 'examples.',
168+ Flatten::FLAG_NUMERIC_NOT_FLATTENED
169+ );
170+
171+ $flattened = $flatten->flatten($examples);
172+
173+ $unflattened = $flatten->unflattenToArray($flattened);
174+
175+ /*
176+ assert($flattened == [
177+ 'examples.templates' => [
178+ [
179+ 'lang' => 'js',
180+ 'template' => 'console.log(\'%s\')';
181+ ],
182+ [
183+ 'lang' => 'php',
184+ 'template' => 'echo "%s"'
185+ ]
186+ ],
187+ 'examples.values' => [
188+ 3 => 'hello world',
189+ 5 => 'what is your name?'
190+ ]
191+ ]);
192+
193+ assert($unflattened == $examples);
173194*/
174195
175196```
@@ -187,31 +208,23 @@ $seats = [
187208 '_blocked' => ['B2']
188209];
189210
190- $flattened = Flatten::flatten($seats, '_', 'seats', Flatten::FLAG_NUMERIC_NOT_FLATTENED);
191-
192- /* print_r($flattened)
211+ $flatten = new Flatten(
212+ '_',
213+ 'seats',
214+ Flatten::FLAG_NUMERIC_NOT_FLATTENED
215+ );
193216
194- Array
195- (
196- [seats] => Array
197- (
198- [0] => A1
199- [1] => A2
200- [2] => B1
201- [3] => B2
202- )
217+ $flattened = $flatten->flatten($seats);
203218
204- [seats_reserved] => Array
205- (
206- [0] => A1
207- [1] => B1
208- )
219+ $unflattened = $flatten->unflattenToArray($flattened);
209220
210- [seats_blocked] => Array
211- (
212- [0] => B2
213- )
214- )
221+ /*
222+ assert($flattened == [
223+ 'seats' => ['A1', 'A2', 'B1', 'B2'],
224+ 'seats_reserved' => ['A1', 'B1'],
225+ 'seats_blocked' => ['B2']
226+ ]);
215227
228+ assert($unflattened == $seats);
216229*/
217230```
0 commit comments