3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $arr = array(); $arr[] = array('p' => 3, 'id' => 4); $arr[] = array('p' => 3, 'id' => 5); $arr[] = array('p' => 3, 'id' => 6); $arr[] = array('p' => 2, 'id' => 3); $arr[] = array('p' => 1, 'id' => 2); $arr[] = array('p' => 1, 'id' => 7); $arr[] = array('p' => 0, 'id' => 1); $mas = array(); foreach($arr as $ind => $itm) { if($itm['p'] == 0) { $mas[$itm['id']]['elements'] = $mas[$itm['id']]; $mas[$itm['id']]['name'] = 'aaa'.$ind; }else{ $mas[$itm['p']][$itm['id']]['elements'] = $mas[$itm['id']]; unset($mas[$itm['id']]); $mas[$itm['p']][$itm['id']]['name'] = 'aaa'.$ind; } } print_r($mas);
Output for 5.3.0 - 5.6.28, 7.0.0 - 7.1.0
Notice: Undefined offset: 4 in /in/Aimcl on line 20 Notice: Undefined offset: 5 in /in/Aimcl on line 20 Notice: Undefined offset: 6 in /in/Aimcl on line 20 Notice: Undefined offset: 7 in /in/Aimcl on line 20 Array ( [1] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) [elements] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) ) [name] => aaa6 ) )
Output for hhvm-3.12.0
Notice: Undefined index: 4 in /in/Aimcl on line -1 Notice: Undefined index: 5 in /in/Aimcl on line -1 Notice: Undefined index: 6 in /in/Aimcl on line -1 Notice: Undefined index: 7 in /in/Aimcl on line -1 Array ( [1] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) [elements] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) ) [name] => aaa6 ) )
Output for hhvm-3.10.0
Notice: Undefined index: 4 in /in/Aimcl on line 20 Notice: Undefined index: 5 in /in/Aimcl on line 20 Notice: Undefined index: 6 in /in/Aimcl on line 20 Notice: Undefined index: 7 in /in/Aimcl on line 20 Array ( [1] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) [elements] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) ) [name] => aaa6 ) )
Output for 4.3.0 - 5.2.17
Notice: Undefined offset: 4 in /in/Aimcl on line 20 Notice: Undefined offset: 5 in /in/Aimcl on line 20 Notice: Undefined offset: 6 in /in/Aimcl on line 20 Notice: Undefined offset: 7 in /in/Aimcl on line 20 Array ( [1] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) [elements] => Array ( [2] => Array ( [elements] => Array ( [3] => Array ( [elements] => Array ( [4] => Array ( [elements] => [name] => aaa0 ) [5] => Array ( [elements] => [name] => aaa1 ) [6] => Array ( [elements] => [name] => aaa2 ) ) [name] => aaa3 ) ) [name] => aaa4 ) [7] => Array ( [elements] => [name] => aaa5 ) ) [name] => aaa6 ) )