$iterator = new RecursiveDirectoryIterator('/path/to/directory'); $recursive = new RecursiveIteratorIterator($iterator); foreach($recursive as $file) { if ($recursive->isDot()) { continue; } // do something with the file }
$iterator = new RecursiveArrayIterator(array('a', 'b', array('c', array('d', 'e')))); $recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST); foreach($recursive as $key => $value) { if ($recursive->isDot()) { continue; } echo $key . ': ' . $value . "\n"; }In this example, we're iterating over a multidimensional array and printing out the keys and values. We're using the SELF_FIRST flag to iterate over the array in a pre-order traversal. Once again, we're skipping over "." and ".." directories. The RecursiveIteratorIterator class is part of PHP's standard library.