From your existing array, it seems that you don't need anything before the last / - if you have 0-1-3-1, then it's obviously going to be found in 0-1-3, which is going to be found in 0-1, which is going to be found in 0. I'm also going to assume it's 0- for all of them. With that in mind I'm going to instead look at the array
[0] => 1
[1] => 1-1
[2] => 1-1-1
[3] => 1-1-1-1
[4] => 1-1-1-2
[5] => 1-1-2
[6] => 1-2
[7] => 1-2-1
[8] => 1-3
[9] => 1-3-1
[10] => 2
[11] => 2-1
[12] => 2-1-1
[13] => 3
[14] => 3-1
[15] => 3-1-1
[16] => 3-2
[17] => 3-2-1
What would be useful would be an array like
array(
'1'=>array(
'1'=>array(
'1'=>array(
'1'=>array(),
'2'=>array(),
),
'2'=>array(),
)
'2'=>array(
'1'=>array()
),
'3'=>array(
'1'=>array()
),
),
'2'=>array(
'1'=>array(
'1'=>array(),
),
),
'3'=>array(
'1'=>array(
'1'=>array(),
),
'2'=>array(
'1'=>array(),
),
),
)
in which the structure of the data was reflected in the data structure.
Then it would be a simple recursive function to walk it
function walk($the_array)
{
foreach($the_array as $name=>$child)
{
start a <div>.
walk($child);
end a <div>.
}
}
Parsing the array of flat text strings into a usable structure is what's left. Assuming it is, as the example shows, sorted appropriately, it shouldn't be too hairy. Ummmm...(remembering I'm using the simplified version):
$parsed_array = array();
foreach($flat_array as $div)
{
$names = explode('-',$div);
$pointer = &$parsed_array;
foreach($names as $name)
{ if(isset($pointer[$name]))
$pointer = &$pointer[$name];
else
$pointer[$name]=array();
}
}
Yup, that looks about right.
print_r($parsed_array);
There's prolly a tidier way of doing it, but that's the first idea I had.