3v4l.org

run code in 300+ PHP versions simultaneously
<?php global $array; $array = [ '0' => [ 'id' => 1, 'parent' => 0, 'name' => 'root 0' ], '1' => [ 'id' => 2, 'parent' => 1, 'name' => 'root 1' ], '2' => [ 'id' => 3, 'parent' => 2, // this should have after parse parent 1 'name' => 'root 2' ], '3' => [ 'id' => 4, 'parent' => 3, // this should have after parse parent 1 'name' => 'root 3' ], '4' => [ 'id' => 5, 'parent' => 3, // this should have after parse parent 1 'name' => 'root 4' ], '5' => [ 'id' => 6, 'parent' => 2, // this should have after parse parent 1 'name' => 'root 2' ] ]; global $new_array; $new_array = []; foreach( $array as $item ) { if( $item['parent'] == 0 ) { $new_array[] = $item; // if parent 0 - clone into new array continue; } //echo $item['name'] . PHP_EOL; $temp = check_parent( $item['parent'] ); // get child $item['parent'] = $temp['id']; $new_array[] = $item; } echo '<pre>'; print_r($new_array); function check_parent( $parent ) { //echo '- check for parent of ' . $parent . PHP_EOL; global $array; foreach( $array as $item ) { if( $item['id'] == $parent && $item['parent'] == 0 ) { //echo '[OK] found root parent id: ' . $item['id'] . PHP_EOL; $item['parent'] = $item['id']; return $item; } else { return check_parent( $item['id'] ); } } }
Output for git.master, git.master_jit, rfc.property-hooks
<pre>Array ( [0] => Array ( [id] => 1 [parent] => 0 [name] => root 0 ) [1] => Array ( [id] => 2 [parent] => 1 [name] => root 1 ) [2] => Array ( [id] => 3 [parent] => 1 [name] => root 2 ) [3] => Array ( [id] => 4 [parent] => 1 [name] => root 3 ) [4] => Array ( [id] => 5 [parent] => 1 [name] => root 4 ) [5] => Array ( [id] => 6 [parent] => 1 [name] => root 2 ) )

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:
116.11 ms | 407 KiB | 5 Q