3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php> $tree = array( 1 => array( 'parent' => 0 ), 2 => array( 'parent' => 1 ), 3 => array( 'parent' => 1 ), 4 => array( 'parent' => 2 ) ); $newtree = array(); foreach($tree as $leaf) { if (!isset($newtree[$leaf['parent']])) $newtree[$leaf['parent']]=array(); $newtree[$leaf['parent']][]=$leaf; } print_r($newtree); </?>
Output for 5.3.0 - 5.4.32
<?php> $tree = array( 1 => array( 'parent' => 0 ), 2 => array( 'parent' => 1 ), 3 => array( 'parent' => 1 ), 4 => array( 'parent' => 2 ) ); $newtree = array(); foreach($tree as $leaf) { if (!isset($newtree[$leaf['parent']])) $newtree[$leaf['parent']]=array(); $newtree[$leaf['parent']][]=$leaf; } print_r($newtree); </?>