A very useful use case for RecusiveIteratorIterator in combination with RecursiveArrayIterator is to replace array values on a multidimensional array at any level deep.

Usually, array_walk_recursive would be used to replace values deep within arrays, but unfortunately this only works when there is a standard key value pair - in other words, array_walk_recursive ONLY VISITS LEAF NODES, NOT arrays.

You can use this to quickly find all the files (recursively) in a certain directory. This beats maintaining a stack yourself.<?php$directory = "/tmp/";$fileSPLObjects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($directory),RecursiveIteratorIterator::CHILD_FIRST);try { foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) { print $fullFileName . " " . $fileSPLObject->getFilename() . "\n"; }}catch (UnexpectedValueException $e) {printf("Directory [%s] contained a directory we can not recurse into", $directory);}?>Note: if there is a directory contained within the directory you are searching in that you have no access to read an UnexpectedValueException will be thrown (leaving you with an empty list).Note: objects returned are SPLFileObjects

This class operates on a tree of elements, which is build by nesting recursive iterators into one another.

Thus you might say it is an iterator over iterators. While traversing those, the class pushes the iterators on a stack while traversing down to a leaf and removes them from the stack while going back up.

Note that this class will first iterate your entire tree and build the graph before going into your foreach. That's propably how it can iterate your leaves first or only your leaves. and that's must be the reason its slower than commom recursive functions.

Beware of that wen you have a tree that can go into a infinite loop, in some cases you know your tree has a infinite loop, but you are looking for something inside it and are breaking the loop wne found, this class wen used on these cases will get stuck into an internal infinite loop.