PHP Doku:: The RecursiveIteratorIterator class - class.recursiveiteratoriterator.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)IteratorenThe RecursiveIteratorIterator class

Ein Service von Reinhard Neidl - Webprogrammierung.

Iteratoren

<<RecursiveFilterIterator::hasChildren

RecursiveIteratorIterator::beginChildren>>


UnterSeiten:

The RecursiveIteratorIterator class

Einführung

Can be used to iterate through recursive iterators.

Klassenbeschreibung

RecursiveIteratorIterator implements OuterIterator , Traversable , Iterator {
/* Methoden */
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 )
/* Geerbte Methoden */
public Iterator OuterIterator::getInnerIterator ( void )
}

Inhaltsverzeichnis


4 BenutzerBeiträge:
- Beiträge aktualisieren...
Tom
6.01.2011 10:35
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.
aidan at php dot net
30.04.2010 6:57
This example demonstrates using the getDepth() method with a RecursiveArrayIterator.

<?php
$tree
= array();
$tree[1][2][3] = 'lemon';
$tree[1][4] = 'melon';
$tree[2][3] = 'orange';
$tree[2][5] = 'grape';
$tree[3] = 'pineapple';

print_r($tree);
 
$arrayiter = new RecursiveArrayIterator($tree);
$iteriter = new RecursiveIteratorIterator($arrayiter);
 
foreach (
$iteriter as $key => $value) {
 
$d = $iteriter->getDepth();
  echo
"depth=$d k=$key v=$value\n";
}
?>

The output of this would be:

Array
(
    [1] => Array
        (
            [2] => Array
                (
                    [3] => lemon
                )

            [4] => melon
        )

    [2] => Array
        (
            [3] => orange
            [5] => grape
        )

    [3] => pineapple
)

depth=2 k=3 v=lemon
depth=1 k=4 v=melon
depth=1 k=3 v=orange
depth=1 k=5 v=grape
depth=0 k=3 v=pineapple
Michiel Brandenburg
15.06.2009 1: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.12.2008 18: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
)



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)