downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

RecursiveIteratorIterator::beginChildren> <RecursiveFilterIterator::hasChildren
Last updated: Fri, 20 Nov 2009

view this page in

The RecursiveIteratorIterator class

Introduction

Can be used to iterate through recursive iterators.

Class synopsis

RecursiveIteratorIterator
RecursiveIteratorIterator implements OuterIterator , Traversable , Iterator {
/* Methods */
public void beginChildren ( void )
public void beginIteration ( void )
public RecursiveIterator callGetChildren ( void )
public bool callHasChildren ( void )
__construct ( Traversable $iterator [, int $mode = LEAVES_ONLY [, int $flags = 0 ]] )
mixed current ( void )
public void endChildren ( void )
public void endIteration ( void )
int getDepth ( void )
public iterator getInnerIterator ( void )
public mixed getMaxDepth ( void )
RecursiveIterator getSubIterator ( void )
mixed key ( void )
void next ( void )
public void nextElement ( void )
void rewind ( void )
public void setMaxDepth ([ string $max_depth = -1 ] )
bool valid ( void )
/* Inherited methods */
public Iterator OuterIterator::getInnerIterator ( void )
}

Table of Contents



add a note add a note User Contributed Notes
RecursiveIteratorIterator
Michiel Brandenburg
15-Jun-2009 01:40
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
crashrox at gmail dot com
19-Dec-2008 06:51
Recursive multidimensional array flatten using SPL

<?php
function array_flatten_recursive($array) {
    if(
$array) {
       
$flat = array();
        foreach(new
RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::SELF_FIRST) as $key=>$value) {
            if(!
is_array($value)) {
               
$flat[] = $value;
            }
        }
       
        return
$flat;
    } else {
        return
false;
    }
}

$array = array(
   
'A' => array('B' => array( 1, 2, 3, 4, 5)),
   
'C' => array( 6,7,8,9)
);

print_r(array_flatten_recursive($array));
?>
-- Returns:
Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

 
show source | credits | sitemap | contact | advertising | mirror sites