PHP 8.5.0 Alpha 4 available for testing

Voting

: five plus one?
(Example: nine)

The Note You're Voting On

catinahat at cool dot fr dot nf
12 years ago
If you need to convert a nested directory tree into a multidimensional array, use this code:

<?php
$ritit
= new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach (
$ritit as $splFileInfo) {
$path = $splFileInfo->isDir()
? array(
$splFileInfo->getFilename() => array())
: array(
$splFileInfo->getFilename());

for (
$depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) {
$path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path);
}
$r = array_merge_recursive($r, $path);
}

print_r($r);
?>

<< Back to user notes page

To Top