3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $strings = "Football / Germany / 1.Liga Football / Germany / 1.Liga Football / Germany / 2.Liga Football / Germany / 2.Liga Football / Germany / 2.Liga Football / England / 1.Liga Football / England / 1.Liga Football / England / 2.Liga Football / England / 2.Liga Football / England / 3.Liga Hockey / Germany / 1.Liga Hockey / Germany / 1.Liga Hockey / Germany / 2.Liga Fechten / Meisterschaft Fechten / Meisterschaft Fechten / Weltmeister"; $lines = explode("\n", $strings); $array = array(); $depth = 0; foreach($lines as $line) { $values = explode(' / ', $line); // find out how deep we have to go if(count($values) > $depth) { $depth = count($values); } } for($i = $depth; $i >= 0; $i--) { // run through lines foreach($lines as $line) { $values = explode(' / ', $line); if(isset($values[$i]) && !in_array($values[$i], $array[$i])) $array[$i][$values[$i-1]] = $values[$i]; //$array[$i][$values[$i]] = $array[$i+1][$values[$i]]; } } var_dump($array);
Output for 5.3.0 - 5.6.28, 7.0.0 - 7.1.0
Notice: Undefined offset: 2 in /in/rJLeZ on line 35 Warning: in_array() expects parameter 2 to be array, null given in /in/rJLeZ on line 35 Notice: Undefined offset: 1 in /in/rJLeZ on line 35 Warning: in_array() expects parameter 2 to be array, null given in /in/rJLeZ on line 35 Notice: Undefined offset: 0 in /in/rJLeZ on line 35 Warning: in_array() expects parameter 2 to be array, null given in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 array(3) { [2]=> array(2) { ["Germany"]=> string(6) "2.Liga" ["England"]=> string(6) "3.Liga" } [1]=> array(3) { ["Football"]=> string(7) "England" ["Hockey"]=> string(7) "Germany" ["Fechten"]=> string(11) "Weltmeister" } [0]=> array(1) { [""]=> string(7) "Fechten" } }
Output for hhvm-3.10.0 - 3.12.0
Notice: Undefined index: 2 in /in/rJLeZ on line 35 Warning: in_array() expects parameter 2 to be an array or collection in /in/rJLeZ on line 35 Notice: Undefined index: 1 in /in/rJLeZ on line 35 Warning: in_array() expects parameter 2 to be an array or collection in /in/rJLeZ on line 35 Notice: Undefined index: 0 in /in/rJLeZ on line 35 Warning: in_array() expects parameter 2 to be an array or collection in /in/rJLeZ on line 35 Notice: Undefined index: -1 in /in/rJLeZ on line 35 Notice: Undefined index: -1 in /in/rJLeZ on line 35 Notice: Undefined index: -1 in /in/rJLeZ on line 35 array(3) { [2]=> array(2) { ["Germany"]=> string(6) "2.Liga" ["England"]=> string(6) "3.Liga" } [1]=> array(3) { ["Football"]=> string(7) "England" ["Hockey"]=> string(7) "Germany" ["Fechten"]=> string(11) "Weltmeister" } [0]=> array(1) { [""]=> string(7) "Fechten" } }
Output for 4.3.2 - 5.2.17
Notice: Undefined offset: 2 in /in/rJLeZ on line 35 Warning: in_array(): Wrong datatype for second argument in /in/rJLeZ on line 35 Notice: Undefined offset: 1 in /in/rJLeZ on line 35 Warning: in_array(): Wrong datatype for second argument in /in/rJLeZ on line 35 Notice: Undefined offset: 0 in /in/rJLeZ on line 35 Warning: in_array(): Wrong datatype for second argument in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 array(3) { [2]=> array(2) { ["Germany"]=> string(6) "2.Liga" ["England"]=> string(6) "3.Liga" } [1]=> array(3) { ["Football"]=> string(7) "England" ["Hockey"]=> string(7) "Germany" ["Fechten"]=> string(11) "Weltmeister" } [0]=> array(1) { [""]=> string(7) "Fechten" } }
Output for 4.3.0 - 4.3.1
Notice: Undefined offset: 2 in /in/rJLeZ on line 35 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/rJLeZ on line 35 Notice: Undefined offset: 1 in /in/rJLeZ on line 35 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/rJLeZ on line 35 Notice: Undefined offset: 0 in /in/rJLeZ on line 35 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 Notice: Undefined offset: -1 in /in/rJLeZ on line 35 array(3) { [2]=> array(2) { ["Germany"]=> string(6) "2.Liga" ["England"]=> string(6) "3.Liga" } [1]=> array(3) { ["Football"]=> string(7) "England" ["Hockey"]=> string(7) "Germany" ["Fechten"]=> string(11) "Weltmeister" } [0]=> array(1) { [""]=> string(7) "Fechten" } }