3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $main = array( array('Women', 'Acces', 'Belts'), array('Women', 'Acces', 'Bangels'), array('Women', 'Top', 'T-Shirts') ); echo "<pre>"; print_r($main); $max_level = 0; foreach($main as $data){ $max_level = count($data); foreach($data as $k => $v) { $level = "lev_$k"; if(!is_array($$level)){ $$level = array(); } $index = array_search($v, $$level); if($index === false){ if($k == 0){ $$level[] = $v; } else { $$parent = "lev_" . ($k-1); if(!is_array($$parent)){ $$parent = array(); } $parent_index = array_search($data[$k-1], $$parent); if($parent_index){ $$level[$parent_index] = $v; } } } } } for($i = 0;$i< $max_level;$i++){ $level = "lev_$i"; print_r($$level); }
based on sHf5T
Output for 7.0.0 - 7.3.0beta1
<pre>Array ( [0] => Array ( [0] => Women [1] => Acces [2] => Belts ) [1] => Array ( [0] => Women [1] => Acces [2] => Bangels ) [2] => Array ( [0] => Women [1] => Top [2] => T-Shirts ) ) Notice: Undefined variable: lev_0 in /in/tV1FA on line 16 Notice: Undefined variable: lev_1 in /in/tV1FA on line 16 Notice: Undefined variable: parent in /in/tV1FA on line 24 Notice: Undefined variable: parent in /in/tV1FA on line 25 Notice: Undefined variable: parent in /in/tV1FA on line 26 Notice: Undefined variable: parent in /in/tV1FA on line 28 Notice: Undefined variable: lev_2 in /in/tV1FA on line 16 Notice: Undefined variable: parent in /in/tV1FA on line 24 Notice: Undefined variable: parent in /in/tV1FA on line 25 Notice: Undefined variable: parent in /in/tV1FA on line 26 Notice: Undefined variable: parent in /in/tV1FA on line 28 Notice: Undefined variable: parent in /in/tV1FA on line 24 Notice: Undefined variable: parent in /in/tV1FA on line 25 Notice: Undefined variable: parent in /in/tV1FA on line 26 Notice: Undefined variable: parent in /in/tV1FA on line 28 Notice: Undefined variable: parent in /in/tV1FA on line 24 Notice: Undefined variable: parent in /in/tV1FA on line 25 Notice: Undefined variable: parent in /in/tV1FA on line 26 Notice: Undefined variable: parent in /in/tV1FA on line 28 Notice: Undefined variable: parent in /in/tV1FA on line 24 Notice: Undefined variable: parent in /in/tV1FA on line 25 Notice: Undefined variable: parent in /in/tV1FA on line 26 Notice: Undefined variable: parent in /in/tV1FA on line 28 Notice: Undefined variable: parent in /in/tV1FA on line 24 Notice: Undefined variable: parent in /in/tV1FA on line 25 Notice: Undefined variable: parent in /in/tV1FA on line 26 Notice: Undefined variable: parent in /in/tV1FA on line 28 Array ( [0] => Women ) Array ( ) Array ( )
Output for 5.6.0 - 5.6.25
Fatal error: Cannot use [] for reading in /in/tV1FA on line 22
Process exited with code 255.