<?php
// Mmmm... functiony goodness
function array_to_toc ($in, &$out, $level = '') {
foreach ($in as $key => $item) { // Loop items
$thisLevel = ($level) ? "$level.".($key + 1) : ($key + 1); // Get this level as string
$out[$thisLevel] = $item['name']; // Add this item to $out
if (isset($item['subs']) && is_array($item['subs']) && count($item['subs'])) array_to_toc($item['subs'],$out,$thisLevel); // Recurse children of this item
}
}
// Here is your test data (slightly modified - I think you stated it wrong in the question)
$array = array (
0 => array (
'name' => 'test1',
'subs' => array (
0 => array (
'name' => 'test2'
),
1 => array (
'name' => 'test3',
'subs' => array (
0 => array (
'name' => 'test4'
)
)
)
)
),
1 => array (
'name' => 'test5'
)
);
// $result is passed by reference and will hold the output after the function has run
$result = array();
array_to_toc($array, $result);
print_r($result);
?>