diff --git a/src/Arrays/Arr.php b/src/Arrays/Arr.php index 8045561..d70b5c1 100644 --- a/src/Arrays/Arr.php +++ b/src/Arrays/Arr.php @@ -1317,7 +1317,7 @@ public static function undot( $array ) { $results = []; foreach ( $array as $key => $value ) { - static::set( $results, $key, $value ); + $results = static::set( $results, explode( '.', $key ), $value ); } return $results; diff --git a/tests/wpunit/UndotTest.php b/tests/wpunit/UndotTest.php new file mode 100644 index 0000000..25218f2 --- /dev/null +++ b/tests/wpunit/UndotTest.php @@ -0,0 +1,105 @@ + 'fibonacci', + ]; + + $expected = [ + 'one' => [ + 'two' => [ + 'three' => [ + 'five' => [ + 'eight' => 'fibonacci' + ] + ] + ] + ], + ]; + + $this->assertSame($expected, Arr::undot($dotted)); + } + + /** + * @test + */ + public function it_expands_array_with_leaves() + { + $dotted = [ + 'one.first_leaf' => true, + 'one.two.second_leaf' => true, + 'one.two.three.third_leaf' => true, + 'one.two.three.five.fifth_leaf' => true, + 'one.two.three.five.eight' => 'fibonacci', + ]; + + $expected = [ + 'one' => [ + 'first_leaf' => true, + 'two' => [ + 'second_leaf' => true, + 'three' => [ + 'third_leaf' => true, + 'five' => [ + 'fifth_leaf' => true, + 'eight' => 'fibonacci' + ] + ] + ] + ], + ]; + + $this->assertSame($expected, Arr::undot($dotted)); + } + + /** + * @test + */ + public function it_expands_nested_arrays_with_numerical_keys() + { + $dotted = [ + 'first_array.0' => 'bacon', + 'first_array.1' => 'ham', + 'first_array.2' => 'cheese', + 'second_array.0' => 'bacon', + 'second_array.1' => 'egg', + 'second_array.2' => 'cheese', + ]; + + $expected = [ + 'first_array' => [ + 'bacon', + 'ham', + 'cheese', + ], + 'second_array' => [ + 'bacon', + 'egg', + 'cheese' + ] + ]; + + $this->assertSame($expected, Arr::undot($dotted)); + } +}