使用PHP文件夹创建HTML菜单

I'm programming a library and want to create a universal HTML menu with the project folders.

I have this structure:

 root     menu folders   sub-menu folders

01_Home     01_aaa          01_aaa, 02_abb, 03_acc
            02_bbb          ...
            03_ccc          ...

$root = 'content';

$fileData = fillArrayWithFileNodes ( new DirectoryIterator ( $root ) );
function fillArrayWithFileNodes(DirectoryIterator $dir) {
    $data = array ();
    foreach ( $dir as $node ) {
        if ($node->isDir () && ! $node->isDot () && $node != 'includes' && $node != 'data') {

            $data [$node->getFilename ()] = fillArrayWithFileNodes ( new DirectoryIterator ( $node->getPathname () ) );
        }
    }
    return $data;
}
function transformName($file) {
    return str_replace ( '_', ' ', substr ( $file, strpos ( $file, '_' ) + 1 ) );
}

And I create the HTML menu here:

<?php
foreach ( array_keys ( $fileData ['01_Home'] ) as $option ) {
?>
<li><a href="content/01_Home/<?php echo $option; ?>"> <strong><?php echo transformName($option); ?></strong></a></li>
<?php
}
?>

Does anyone know how I can display the sub-menu folder names?

Like what you did with fillArrayWithNodes, I would create a helper function that displays all the subnodes of a specific folder array, e.g. displayChildNodes($folder)

That function will take in an associative array from $fileData, e.g. $fileData['01_Home']. It will contain a similar for loop like fillArrayWithNodes, except this time we are checking if there is data, given that key. We check that by determining if it is an array (hence it would fit the if condition in fillArrayWithFileNodes), and if there are items in the array.

foreach($folderArray as $subNodeName => $subNodeNodes) {
    //1. Print out the folder name $subNodeName here. (like you did in the example)  
    //2. if is_array($subNodeNodes) && count($subNodeNodes) > 0, then call displayChildNodes($subNodeNodes) 
}