Класс RecursiveArrayIterator

(PHP 5 >= 5.1.0)

Введение

Этот итератор позволяет сбросить и изменить значения и ключи во время прохода по массивам и объектам таким же образом, как и ArrayIterator. Кроме того, можно перебирать текущие записи итератора.

Обзор классов

RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {
/* Методы */
public RecursiveArrayIterator getChildren ( void )
public bool hasChildren ( void )
/* Inherits */
public void ArrayIterator::append ( mixed $value )
public void ArrayIterator::asort ( void )
public ArrayIterator::__construct ([ mixed $array = array() [, int $flags = 0 ]] )
public int ArrayIterator::count ( void )
public mixed ArrayIterator::current ( void )
public array ArrayIterator::getArrayCopy ( void )
public void ArrayIterator::getFlags ( void )
public mixed ArrayIterator::key ( void )
public void ArrayIterator::ksort ( void )
public void ArrayIterator::natcasesort ( void )
public void ArrayIterator::natsort ( void )
public void ArrayIterator::next ( void )
public void ArrayIterator::offsetExists ( string $index )
public mixed ArrayIterator::offsetGet ( string $index )
public void ArrayIterator::offsetSet ( string $index , string $newval )
public void ArrayIterator::offsetUnset ( string $index )
public void ArrayIterator::rewind ( void )
public void ArrayIterator::seek ( int $position )
public string ArrayIterator::serialize ( void )
public void ArrayIterator::setFlags ( string $flags )
public void ArrayIterator::uasort ( string $cmp_function )
public void ArrayIterator::uksort ( string $cmp_function )
public string ArrayIterator::unserialize ( string $serialized )
public bool ArrayIterator::valid ( void )
}

Содержание

  • RecursiveArrayIterator::getChildren — Возвращает итератор для текущего элемента, если этот элемент является массивом (array) или объектом (object)
  • RecursiveArrayIterator::hasChildren — Определяет, является ли текущий элемент массивом или объектом

Коментарии

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(
   
=> 'a',
   
=> array('subA','subB',array(=> 'subsubA'=> 'subsubB'=> array(=> 'deepA'=> 'deepB'))),
   
=> 'b',
   
=> array('subA','subB','subC'),
   
=> '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
2011-02-22 09:23:56
http://php5.kiev.ua/manual/ru/class.recursivearrayiterator.html
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!
2011-11-15 12:29:17
http://php5.kiev.ua/manual/ru/class.recursivearrayiterator.html
The RecursiveArrayOnlyIterator behaviour c dot 1 at smithies dot org presented can also be achieved using the (undocumented) flag RecursiveArrayIterator::CHILD_ARRAYS_ONLY (https://github.com/php/php-src/blob/master/ext/spl/spl_array.c#L1970 and https://github.com/php/php-src/blob/master/ext/spl/spl_array.c#L1620)
2013-12-13 00:29:01
http://php5.kiev.ua/manual/ru/class.recursivearrayiterator.html
Автор:
<?php 
$array 
= [
'A','B',
'C'=>[
   
'D','E',
   
'F'=>['G','H']
 ],
'I','J'
];

$iterator = new RecursiveArrayIterator($array);

foreach(
$iterator as $key=>$value)
{
    echo 
$key,':'$value,'<br>';
}

/**
Output
0:A
1:B
C:Array
2:I
3:J
*/

//-------------
//Recursive...

$array = [
'A','B',
'C'=>[
   
'D','E',
   
'F'=>['G','H']
 ],
'I','J'
];

$it = new RecursiveArrayIterator($array);
$iterator = new RecursiveIteratorIterator($it);

foreach(
$iterator as $key=>$value)
{
    echo 
$key,':'$value,'<br>';
}

/**
Output
0:A
1:B
0:D
1:E
0:G
1:H
2:I
3:J
*/

?>
2022-05-12 14:47:26
http://php5.kiev.ua/manual/ru/class.recursivearrayiterator.html

    Поддержать сайт на родительском проекте КГБ