Skip to content

Commit d84f8e6

Browse files
committed
Implemented unflatten() method
1 parent 61652db commit d84f8e6

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ will return:
286286
<a href="#uasorted">uasorted</a>
287287
<a href="#uksort">uksort</a>
288288
<a href="#uksorted">uksorted</a>
289+
<a href="#unflatten">unflatten</a>
289290
<a href="#union">union</a>
290291
<a href="#unique">unique</a>
291292
<a href="#unshift">unshift</a>
@@ -498,6 +499,7 @@ will return:
498499
* [transpose()](#transpose) : Exchanges rows and columns for a two dimensional map
499500
* [traverse()](#traverse) : Traverses trees of nested items passing each item to the callback
500501
* [trim()](#trim) : Removes the passed characters from the left/right of all strings
502+
* [unflatten()](#unflatten) : Unflattens the key path/value pairs into a multi-dimensional array
501503
* [walk()](#walk) : Applies the given callback to all elements
502504
* [zip()](#zip) : Merges the values of all arrays at the corresponding index
503505

@@ -6965,6 +6967,32 @@ Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( function( $keyA, $keyB ) {
69656967
* [uksort()](#uksort) - Sorts the map elements by their keys using a callback
69666968

69676969

6970+
### unflatten()
6971+
6972+
Unflattens the key path/value pairs into a multi-dimensional array.
6973+
6974+
```php
6975+
public function unflatten() : self
6976+
```
6977+
6978+
* @return **self&#60;string,mixed&#62;** New map with multi-dimensional arrays
6979+
6980+
**Examples:**
6981+
6982+
```php
6983+
Map::from( ['a/b/c' => 1, 'a/b/d' => 2, 'b/e' => 3] )->unflatten();
6984+
// ['a' => ['b' => ['c' => 1, 'd' => 2]], 'b' => ['e' => 3]]
6985+
6986+
Map::from( ['a.b.c' => 1, 'a.b.d' => 2, 'b.e' => 3] )->sep( '.' )->unflatten();
6987+
// ['a' => ['b' => ['c' => 1, 'd' => 2]], 'b' => ['e' => 3]]
6988+
```
6989+
6990+
**See also:**
6991+
6992+
* [flat()](#flat) - Flattens multi-dimensional elements without overwriting elements
6993+
* [collapse()](#collapse) - Collapses all sub-array elements recursively to a new map
6994+
6995+
69686996
### union()
69696997

69706998
Builds a union of the elements and the given elements without returning a new map.

src/Map.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4254,7 +4254,7 @@ public function search( $value, $strict = true )
42544254
* delimiter() method instead.
42554255
*
42564256
* Examples:
4257-
* Map::from( ['foo' => ['bar' => 'baz']] )->sep( '/' )->get( 'foo/bar' );
4257+
* Map::from( ['foo' => ['bar' => 'baz']] )->sep( '.' )->get( 'foo.bar' );
42584258
*
42594259
* Results:
42604260
* 'baz'
@@ -5800,6 +5800,38 @@ public function uksorted( callable $callback ) : self
58005800
}
58015801

58025802

5803+
/**
5804+
* Unflattens the key path/value pairs into a multi-dimensional array.
5805+
*
5806+
* Examples:
5807+
* Map::from( ['a/b/c' => 1, 'a/b/d' => 2, 'b/e' => 3] )->unflatten();
5808+
* Map::from( ['a.b.c' => 1, 'a.b.d' => 2, 'b.e' => 3] )->sep( '.' )->unflatten();
5809+
*
5810+
* Results:
5811+
* ['a' => ['b' => ['c' => 1, 'd' => 2]], 'b' => ['e' => 3]]
5812+
*
5813+
* @return self<int|string,mixed> New map with multi-dimensional arrays
5814+
*/
5815+
public function unflatten() : self
5816+
{
5817+
$result = [];
5818+
5819+
foreach( $this->list() as $key => $value )
5820+
{
5821+
$nested = &$result;
5822+
$parts = explode( $this->sep, $key );
5823+
5824+
while( count( $parts ) > 1 ) {
5825+
$nested = &$nested[array_shift( $parts )] ?? [];
5826+
}
5827+
5828+
$nested[array_shift( $parts )] = $value;
5829+
}
5830+
5831+
return new static( $result );
5832+
}
5833+
5834+
58035835
/**
58045836
* Builds a union of the elements and the given elements without overwriting existing ones.
58055837
* Existing keys in the map will not be overwritten

tests/MapTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,6 +3874,16 @@ public function testUsorted()
38743874
}
38753875

38763876

3877+
public function testUnflatten()
3878+
{
3879+
$flat = ['a/b/c' => 1, 'a/b/d' => 2, 'b/e' => 3];
3880+
$exp = ['a' => ['b' => ['c' => 1, 'd' => 2]], 'b' => ['e' => 3]];
3881+
3882+
$this->assertSame( $exp, Map::from( ['a/b/c' => 1, 'a/b/d' => 2, 'b/e' => 3] )->unflatten()->toArray() );
3883+
$this->assertSame( $exp, Map::from( ['a.b.c' => 1, 'a.b.d' => 2, 'b.e' => 3] )->sep( '.' )->unflatten()->toArray() );
3884+
}
3885+
3886+
38773887
public function testUnionArray()
38783888
{
38793889
$m = new Map( ['name' => 'Hello'] );

0 commit comments

Comments
 (0)