- var_dump: documentation ( source)
- explode: documentation ( source)
- in_array: documentation ( source)
<?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);