File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,7 @@ Dot has the following methods:
73
73
- [ clear()] ( #clear )
74
74
- [ count()] ( #count )
75
75
- [ delete()] ( #delete )
76
+ - [ flatten()] ( #flatten )
76
77
- [ get()] ( #get )
77
78
- [ has()] ( #has )
78
79
- [ isEmpty()] ( #isEmpty )
@@ -175,6 +176,14 @@ $dot->delete([
175
176
]);
176
177
```
177
178
179
+ <a name =" flatten " ></a >
180
+ ### flatten()
181
+
182
+ Returns a flattened array with the keys delimited by a given character (default "."):
183
+ ``` php
184
+ $flatten = $dot->flatten();
185
+ ```
186
+
178
187
<a name =" get " ></a >
179
188
### get()
180
189
Original file line number Diff line number Diff line change @@ -132,6 +132,36 @@ protected function exists($array, $key)
132
132
return array_key_exists ($ key , $ array );
133
133
}
134
134
135
+ /**
136
+ * Flatten an array with the given character as a key delimiter
137
+ *
138
+ * @param string $delimiter
139
+ * @param array|null $items
140
+ * @param string $prepend
141
+ * @return array
142
+ */
143
+ public function flatten ($ delimiter = '. ' , $ items = null , $ prepend = '' )
144
+ {
145
+ $ flatten = [];
146
+
147
+ if (is_null ($ items )) {
148
+ $ items = $ this ->items ;
149
+ }
150
+
151
+ foreach ($ items as $ key => $ value ) {
152
+ if (is_array ($ value ) && !empty ($ value )) {
153
+ $ flatten = array_merge (
154
+ $ flatten ,
155
+ $ this ->flatten ($ delimiter , $ value , $ prepend .$ key .$ delimiter )
156
+ );
157
+ } else {
158
+ $ flatten [$ prepend .$ key ] = $ value ;
159
+ }
160
+ }
161
+
162
+ return $ flatten ;
163
+ }
164
+
135
165
/**
136
166
* Return the value of a given key
137
167
*
Original file line number Diff line number Diff line change @@ -170,6 +170,27 @@ public function testDeleteArrayOfKeys()
170
170
$ this ->assertSame ([], $ dot ->all ());
171
171
}
172
172
173
+ /*
174
+ * --------------------------------------------------------------
175
+ * Flatten
176
+ * --------------------------------------------------------------
177
+ */
178
+ public function testFlatten ()
179
+ {
180
+ $ dot = new Dot (['foo ' => ['abc ' => 'xyz ' , 'bar ' => ['baz ' ]]]);
181
+ $ flatten = $ dot ->flatten ();
182
+ $ this ->assertEquals ('xyz ' , $ flatten ['foo.abc ' ]);
183
+ $ this ->assertEquals ('baz ' , $ flatten ['foo.bar.0 ' ]);
184
+ }
185
+
186
+ public function testFlattenWithCustomDelimiter ()
187
+ {
188
+ $ dot = new Dot (['foo ' => ['abc ' => 'xyz ' , 'bar ' => ['baz ' ]]]);
189
+ $ flatten = $ dot ->flatten ('_ ' );
190
+ $ this ->assertEquals ('xyz ' , $ flatten ['foo_abc ' ]);
191
+ $ this ->assertEquals ('baz ' , $ flatten ['foo_bar_0 ' ]);
192
+ }
193
+
173
194
/*
174
195
* --------------------------------------------------------------
175
196
* Get
You can’t perform that action at this time.
0 commit comments