3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { public function getData() { return [ 1 => [ 'fooa1' => 'bara1', 'fooa2' => 'bara2', 'fooa3' => 'bara3', 'parent' => 0 ], 2 => [ 'food1' => 'bard1', 'food2' => 'bard2', 'food3' => 'bard3', 'parent' => 1 ], 3 => [ 'fooc1' => 'barc1', 'fooc2' => 'barc2', 'fooc3' => 'barc3', 'parent' => 1 ], ]; } public function getDataPattern() { return [ 'a' => [ 'fooa1' => 'bara1', 'fooa2' => 'bara2', 'fooa3' => 'bara3', 'children' => [ 'd' => [ 'food1' => 'bard1', 'food2' => 'bard2', 'food3' => 'bard3', 'parent' => 'a' ], ] ], 'b' => [ 'foob1' => 'barb1', 'foob2' => 'barb2', 'foob3' => 'barb3', 'children' => [ 'c' => [ 'fooc1' => 'barc1', 'fooc2' => 'barc2', 'fooc3' => 'barc3', 'parent' => 'b', 'children' => [ 'e' => [ 'fooe1' => 'bare1', 'fooe2' => 'bare2', 'fooe3' => 'bare3', 'parent' => 'c' ], ] ], ] ], ]; } public function transformArrayToPattern() { $recordsObject = json_decode(json_encode($this->getData())); foreach ($recordsObject as $key => $obj) { if ($obj->parent !== 0) { $recordsObject->{$obj->parent}->children[$key] = $obj; } } return array_filter((array) $recordsObject, function ($record) { return $record->parent === 0; }); } } print_r( (new Foo)->transformArrayToPattern() );
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [1] => stdClass Object ( [fooa1] => bara1 [fooa2] => bara2 [fooa3] => bara3 [parent] => 0 [children] => Array ( [2] => stdClass Object ( [food1] => bard1 [food2] => bard2 [food3] => bard3 [parent] => 1 ) [3] => stdClass Object ( [fooc1] => barc1 [fooc2] => barc2 [fooc3] => barc3 [parent] => 1 ) ) ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
49.75 ms | 1653 KiB | 4 Q