Righto. I'm going to take the liberty of modifying the structure a wee bit (the code that does this will be below). I'll be turning
Array
(
[1] => Array
(
[f] => 0
[t] => Printer, Scanner
)
[2] => Array
(
[f] => 1
[t] => Laser Colour
)
[3] => Array
(
[f] => 1
[t] => Dot Matrix
)
[4] => Array
(
[f] => 1
[t] => Print & Fax Server
)
[5] => Array
(
[f] => 1
[t] => Accessories
)
[6] => Array
(
[f] => 1
[t] => Laser Mono
)
[7] => Array
(
[f] => 1
[t] => Scanner
)
[8] => Array
(
[f] => 1
[t] => All In One
)
[9] => Array
(
[f] => 1
[t] => Bubble Jet
)
[10] => Array
(
[f] => 0
[t] => Camera & Photo
)
[11] => Array
(
[f] => 10
[t] => Digital Still
)
[12] => Array
(
[f] => 11
[t] => Accessories
)
)
into
Array
(
[0] => Array
(
[1] => Printer, Scanner
[10] => Camera & Photo
)
[1] => Array
(
[2] => Laser Colour
[3] => Dot Matrix
[4] => Print & Fax Server
[5] => Accessories
[6] => Laser Mono
[7] => Scanner
[8] => All In One
[9] => Bubble Jet
)
[10] => Array
(
[11] => Digital Still
)
[11] => Array
(
[12] => Accessories
)
)
The root is element number 0, and each element of this array is the root of a subtree; its children have their original keys, and each appears in the subtree that has the same key as its parent (so, for example "Accessories" with ID 12, appears in subtree 11, because it's a child of the item with ID 11 - "Camera & Photo").
Anyway, the code.
function convert_structure($original_array)
{
$tree = array();
foreach($original_array as $key=>$item)
{
$tree[$item['f']][$key]=$item['t'];
}
return $tree;
}
$tree = convert_structure($original_array);
// Supply a value for $root if you don't want to start from the real root every time.
function printList($tree, $root=0)
{
if(!isset($tree[$root]))return;
foreach($tree[$root] as $key=>$item)
{
echo "<li>".$item;
if(isset($tree[$key]))
{
echo "<ul>\n";
printList($tree, $key);
echo "</ul>";
}
echo "</li>\n";
}
}
echo "<ul>";
printList($tree);
echo "</ul>";