I rarely write code for this sort of thing but this one was a challenge
This will make a multi-dimensional array with each dir id.
<?php
$list = array();
$list[] = array('dir'=>1,'indir'=>0, 'name'=>'Dir1');
$list[] = array('dir'=>2,'indir'=>0, 'name'=>'Dir2');
$list[] = array('dir'=>3,'indir'=>1, 'name'=>'Dir3');
$list[] = array('dir'=>4,'indir'=>3, 'name'=>'Dir4');
$list[] = array('dir'=>5,'indir'=>1, 'name'=>'Dir5');
$main = getLevels(0);
//Recursive function to get any subdirectories.
function getLevels($levels){
global $list;
foreach($list AS $key=>$listed){
if($listed['indir'] == $levels){
$subdir[$listed['dir']] = getLevels($listed['dir']);
unset($list[$k]);
}
}
return $subdir;
}
print_r($main);
?>
See it in action:
http://www.grady.us/test/dirs.php