A custom PHP function to recursively iterate over directories and output a hierarchical, multidimensional array?
Using the new SPL Iterator class (RecursiveIterator*), I've been working on the following function:
function directoryToArray( $directory ) {
$array = [];
$objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $directory ), RecursiveIteratorIterator::SELF_FIRST );
foreach ( $objects as $name => $object ) {
if ( !( $object->getFilename() == "." | $object->getFilename() == ".." ) ) {
$path = $object->isDir() ? [
[
'name' => $object->getFilename(),
'file' => $object->getFilename(),
'children' => []
]
] : [
[
'name' => friendlyName( $object->getFilename() ),
'file' => $object->getFilename(),
]
];
for ( $depth = $objects->getDepth() - 1; $depth >= 0; $depth-- ) {
$path = [
$objects->getSubIterator( $depth )->current()->getFilename() => $path,
];
}
$array = array_merge_recursive( $array, $path );
}
}
return $array;
}
This currently results in the following output:
Array
(
[0] => Array
(
[name] => Anchor Links
[file] => anchor-links.php
)
[1] => Array
(
[name] => Columns
[file] => columns.php
)
[2] => Array
(
[name] => Page Layouts
[file] => page-layouts
[children] => Array
(
)
)
[page-layouts] => Array
(
[0] => Array
(
[name] => Right Sidebar
[file] => right-sidebar.php
)
[1] => Array
(
[name] => Left Sidebar
[file] => left-sidebar.php
)
[2] => Array
(
[name] => Right Sidebar
[file] => right-sidebar
[children] => Array
(
)
)
[right-sidebar] => Array
(
[0] => Array
(
[name] => Other Options
[file] => other-options.php
)
[1] => Array
(
[name] => Option A
[file] => option-a.php
)
[2] => Array
(
[name] => Other Options
[file] => other-options
[children] => Array
(
)
)
[other-options] => Array
(
[0] => Array
(
[name] => Sample
[file] => sample.php
)
)
)
[3] => Array
(
[name] => Changelog
[file] => changelog.php
)
)
)
However, the output I'm trying to achieve is the following:
Array
(
[0] => Array
(
[name] => Anchor Links
[file] => anchor-links.php
)
[1] => Array
(
[name] => Columns
[file] => columns.php
)
[2] => Array
(
[name] => Page Layouts
[file] => page-layouts
[children] => Array
(
[0] => Array
(
[name] => Right Sidebar
[file] => right-sidebar.php
)
[1] => Array
(
[name] => Left Sidebar
[file] => left-sidebar.php
)
[2] => Array
(
[name] => Right Sidebar
[file] => right-sidebar
[children] => Array
(
[0] => Array
(
[name] => Other Options
[file] => other-options.php
)
[1] => Array
(
[name] => Option A
[file] => option-a.php
)
[2] => Array
(
[name] => Other Options
[file] => other-options
[children] => Array
(
[0] => Array
(
[name] => Sample
[file] => sample.php
)
)
)
)
)
[3] => Array
(
[name] => Changelog
[file] => changelog.php
)
)
)
)
I think we're almost there, I'm just stumped on how to get the subdirectories to output to the (now) empty array inside of [children]. Also, do not be confused about the duplicate or similar names (e.g., there is a right-sidebar.php file and a right-sidebar subdirectory in the same directory).