<?php $fruits[] = array("fruits", "yellow", "pineapple"); $fruits[] = array("fruits", "yellow", "lemon"); $fruits[] = array("fruits", "red", "apple"); $fruits[] = array("fruits", "blue", "small","blueberry"); $fruits[] = array("fruits", "blue", "bluefruit"); $fruits[] = array("fruits", "multicolor-fruit"); function deeper(&$multifruit, $fruit) { if (count($fruit)>2) { $shifted = array_shift($fruit); deeper($multifruit[$shifted], $fruit); return $multifruit; } else { return $multifruit[$fruit[0]][] = $fruit[1]; } } foreach($fruits as $fruit) { deeper($multifruit, $fruit); } print_r($multifruit); ?>
Here you will find a more general solution to your problem. It took me a while, so I hope you appreciate it :)
source share