La clase RecursiveArrayIterator

(PHP 5 >= 5.1.0, PHP 7, PHP 8)


Este iterador permite destruir y modificar valores y claves mientras se iteran arrays y objetos de la misma manera que con ArrayIterator. Adicionalmente es posible iterar la entrada del iterador actual.

Sinopsis de la Clase

class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {
/* Constantaes heredadas constants */
const integer STD_PROP_LIST = 1;
const integer ARRAY_AS_PROPS = 2;
/* Constantes */
const integer CHILD_ARRAYS_ONLY = 4;
/* Métodos */
public hasChildren(): bool
/* Inherits */
public ArrayIterator::append(mixed $value): void
public ArrayIterator::asort(): void
public ArrayIterator::__construct(mixed $array = array(), int $flags = 0)
public ArrayIterator::count(): int
public ArrayIterator::ksort(): void
public ArrayIterator::natsort(): void
public ArrayIterator::next(): void
public ArrayIterator::offsetExists(mixed $index): bool
public ArrayIterator::offsetSet(mixed $index, mixed $newval): void
public ArrayIterator::offsetUnset(mixed $index): void
public ArrayIterator::rewind(): void
public ArrayIterator::seek(int $position): void
public ArrayIterator::serialize(): string
public ArrayIterator::setFlags(string $flags): void
public ArrayIterator::uasort(string $cmp_function): void
public ArrayIterator::uksort(string $cmp_function): void
public ArrayIterator::unserialize(string $serialized): string
public ArrayIterator::valid(): bool

Constantes predefinidas

RecursiveArrayIterator Flags


Trata sólo los array (no objetos) como si tuvieran hijos para la itaración recursiva.

Historial de cambios

Versión Descripción
5.3.0 Se añadió la flag CHILD_ARRAYS_ONLY.

Tabla de contenidos

add a note add a note

User Contributed Notes 3 notes

c dot 1 at smithies dot org
11 years ago
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:

class RecursiveArrayOnlyIterator extends RecursiveArrayIterator {
  public function
hasChildren() {
Of course, this simple example will not recurse into ArrayObjects either!
mccarthy dot richard at gmail dot com
12 years ago
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!

= 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));

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

        if (
$iterator -> hasChildren() ) {
traverseStructure($iterator -> getChildren());
        else {
$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
lemoinem dot remove at me dot mlemoine dot name
9 years ago
The RecursiveArrayOnlyIterator behaviour c dot 1 at smithies dot org presented can also be achieved using the (undocumented) flag RecursiveArrayIterator::CHILD_ARRAYS_ONLY ( and
To Top