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

search for in the

RecursiveArrayIterator::getChildren> <ParentIterator::rewind
[edit] Last updated: Fri, 18 Sep 2009

view this page in

The RecursiveArrayIterator class

Увод

This iterator allows to unset and modify values and keys while iterating over Arrays and Objects in the same way as the ArrayIterator. Additionally it is possible to iterate over the current iterator entry.

Синтаксис за класове

RecursiveArrayIterator
RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {
/* Methods */
public RecursiveArrayIterator getChildren ( void )
public bool hasChildren ( void )
/* Inherits */
public void ArrayIterator::append ( string $value )
public void ArrayIterator::asort ( void )
ArrayIterator::__construct ( string $array )
public void ArrayIterator::count ( void )
mixed ArrayIterator::current ( void )
public void ArrayIterator::getArrayCopy ( void )
public void ArrayIterator::getFlags ( void )
mixed ArrayIterator::key ( void )
public void ArrayIterator::ksort ( void )
public void ArrayIterator::natcasesort ( void )
public void ArrayIterator::natsort ( void )
void ArrayIterator::next ( void )
public void ArrayIterator::offsetExists ( string $index )
void ArrayIterator::rewind ( void )
void ArrayIterator::seek ( int $position )
bool ArrayIterator::valid ( void )
}

Съдържание



add a note add a note User Contributed Notes RecursiveArrayIterator
c dot 1 at smithies dot org 15-Nov-2011 03:29
If you are iterating over a multi-dimensional array of objects, you may be tempted to use a RecursiveArrayIterator within a RecursiveIteratorIterator. You are likely to get baffling results if you do. That is because RecursiveArrayIterator treats all objects as having children, and tries to recurse into them. But if you are interested in having your RecursiveIteratorIterator return the objects in your multi-dimensional array, then you don't want the default setting LEAVES_ONLY, because no object can be a leaf (= has no children).

The solution is to extend the RecursiveArrayIterator class and override the hasChildren method appropriately. Something like the following might be suitable:

<?php
class RecursiveArrayOnlyIterator extends RecursiveArrayIterator {
  public function
hasChildren() {
    return
is_array($this->current());
  }
}
?>
Of course, this simple example will not recurse into ArrayObjects either!
mccarthy dot richard at gmail dot com 22-Feb-2011 12:23
Using the RecursiveArrayIterator to traverse an unknown amount of sub arrays within the outer array. Note: This functionality is already provided by using the RecursiveIteratorIterator but is useful in understanding how to use the iterator when using for the first time as all the terminology does get rather confusing at first sight of SPL!

<?php
$myArray
= array(
   
0 => 'a',
   
1 => array('subA','subB',array(0 => 'subsubA', 1 => 'subsubB', 2 => array(0 => 'deepA', 1 => 'deepB'))),
   
2 => 'b',
   
3 => array('subA','subB','subC'),
   
4 => 'c'
);

$iterator = new RecursiveArrayIterator($myArray);
iterator_apply($iterator, 'traverseStructure', array($iterator));

function
traverseStructure($iterator) {
   
    while (
$iterator -> valid() ) {

        if (
$iterator -> hasChildren() ) {
       
           
traverseStructure($iterator -> getChildren());
           
        }
        else {
            echo
$iterator -> key() . ' : ' . $iterator -> current() .PHP_EOL;   
        }

       
$iterator -> next();
    }
}
?>

The output from which is:
0 : a
0 : subA
1 : subB
0 : subsubA
1 : subsubB
0 : deepA
1 : deepB
2 : b
0 : subA
1 : subB
2 : subC
4 : c

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