@@ -51,7 +51,7 @@ public function __construct(
5151 }
5252
5353 /**
54- * Flattens a traversable or array into a 1-dimensional array .
54+ * Flattens an iterable into a 1-dimensional generator .
5555 *
5656 * Each key (fully-qualified key or FQK) in the returned one-dimensional array is the join of all keys leading to
5757 * each (non-traversable) value, in all dimensions, separated by the configured separator.
@@ -68,18 +68,33 @@ public function flatten($var)
6868 }
6969 }
7070
71+ /**
72+ * Unflattens a 1-dimensional iterable into a multi-dimensional generator.
73+ *
74+ * Fully Qualitifed Keys (FQks) in the input array will be split by the
75+ * configured separator, and resulting splits will form keys for each level
76+ * down the resulting multi-dimensional array.
77+ *
78+ * The configured prefix will be removed from FQKs in the input array first.
79+ *
80+ * The resulting generator can be recursively converted into a final array
81+ * using the utility class `TraversableToArray` which accounts for the fact
82+ * that generatos may yield same key with different values and combine these
83+ * values into an array under that key as expected.
84+ *
85+ * @param mixed $var
86+ * @return multi-dimensional generator.
87+ * @see Util\TraversableToArray
88+ * @see unflattenToArray
89+ */
7190 public function unflatten ($ var )
7291 {
7392 if (!$ this ->canTraverse ($ var )) {
7493 yield $ var ;
7594 }
7695
7796 foreach ($ var as $ key => $ value ) {
78- if (!empty ($ this ->prefix )
79- && substr ($ key , 0 , strlen ($ this ->prefix )) === $ this ->prefix
80- ) {
81- $ key = substr ($ key , strlen ($ this ->prefix ));
82- }
97+ $ key = substr ($ key , strlen ($ this ->prefix ));
8398
8499 if (!empty ($ key )) {
85100 foreach ($ this ->unflattenGenerator ($ key , $ value ) as $ k => $ v ) {
@@ -97,6 +112,18 @@ public function unflatten($var)
97112 }
98113 }
99114
115+ /**
116+ * Unflattens a 1-dimensional iterable into a multi-dimensional array.
117+ *
118+ * @param mixed $var
119+ * @return array
120+ * @see flatten
121+ */
122+ public function unflattenToArray ($ var )
123+ {
124+ return Util \TraversableToArray::toArray ($ this ->unflatten ($ var ));
125+ }
126+
100127 private function flattenGenerator ($ var , $ prefix )
101128 {
102129 if (!$ this ->canTraverse ($ var )) {
0 commit comments