Well, to recursively RecursiveIterator over a RecursiveIterator , you need a RecursiveIteratorIterator (I know it is superfluous, but it is not) ...
However, for your specific case (where you want to create a structure, and not just visit all nodes), I think regular recursion would be better suited ...
function DirectoryIteratorToArray(DirectoryIterator $it) { $result = array(); foreach ($it as $key => $child) { if ($child->isDot()) { continue; } $name = $child->getBasename(); if ($child->isDir()) { $subit = new DirectoryIterator($child->getPathname()); $result[$name] = DirectoryIteratorToArray($subit); } else { $result[] = $name; } } return $result; }
Edit it to work with non-recursive iterators ...
source share