3v4l.org

run code in 200+ 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);