3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $input = array( array('id' => 2, 'name' => 'Clement', 'upper' => 0), array('id' => 1, 'name' => 'Jean', 'upper' => 1), array('id' => 1, 'name' => 'Annie', 'upper' => 1) ); $map = array(); foreach ($input as $node) { // init self if (!array_key_exists($node['sn'], $map)) { $map[$node['sn']] = array('self' => $node['name']); } else { $map[$node['sn']]['self'] = $node['name']; } // init parent if (!array_key_exists($node['upper'], $map)) { $map[$node['upper']] = array(); } // add to parent $map[$node['upper']][$node['sn']] = & $map[$node['sn']]; } print_r($map[0]);
based on uuUTM
Output for 5.3.0 - 7.2.0
Notice: Undefined index: sn in /in/LJN4H on line 12 Notice: Undefined index: sn in /in/LJN4H on line 13 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 12 Notice: Undefined index: sn in /in/LJN4H on line 16 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 12 Notice: Undefined index: sn in /in/LJN4H on line 16 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 25 Array ( [] => Array ( [self] => Annie ) )
Output for 4.3.0 - 5.2.17
Notice: Undefined index: sn in /in/LJN4H on line 12 Notice: Undefined index: sn in /in/LJN4H on line 13 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 12 Notice: Undefined index: sn in /in/LJN4H on line 16 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 12 Notice: Undefined index: sn in /in/LJN4H on line 16 Notice: Undefined index: sn in /in/LJN4H on line 25 Notice: Undefined index: sn in /in/LJN4H on line 25 Array ( [] => Array ( [self] => Annie ) )