3v4l.org

run code in 300+ PHP versions simultaneously
<?php $list = [ [ 'name' => 'blah', 'path' => 'A' ], [ 'name' => 'blah', 'path' => 'AA' ], [ 'name' => 'blah', 'path' => 'AB' ], [ 'name' => 'blah', 'path' => 'B' ], [ 'name' => 'blah', 'path' => 'BA' ], [ 'name' => 'blah', 'path' => 'BAA' ], ]; $result = array_reduce($list, function ($a, $c) { $l = substr($c['path'], 0, 1); isset($a[$l]) ? $a[$l]['children'][] = $c : $a[$l] = [ 'name' => $c['name'], 'path' => $c['path'], 'children' => [] ]; return $a; }, []); // to remove the letter keys, uncomment the next line // $result = array_values($result); var_export($result);
Output for git.master, git.master_jit, rfc.property-hooks
array ( 'A' => array ( 'name' => 'blah', 'path' => 'A', 'children' => array ( 0 => array ( 'name' => 'blah', 'path' => 'AA', ), 1 => array ( 'name' => 'blah', 'path' => 'AB', ), ), ), 'B' => array ( 'name' => 'blah', 'path' => 'B', 'children' => array ( 0 => array ( 'name' => 'blah', 'path' => 'BA', ), 1 => array ( 'name' => 'blah', 'path' => 'BAA', ), ), ), )

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