PHPのお勉強!

PHP TOP

RecursiveArrayIterator クラス

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

はじめに

このイテレータは、ArrayIterator と同じように配列やオブジェクトを反復処理しつつ、 そのキーや値を消去したり書き換えたりすることができます。 さらに、現在のイテレータのエントリを反復処理することも可能です。

クラス概要

class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {
/* 継承した定数 */
/* 定数 */
public const int CHILD_ARRAYS_ONLY;
/* メソッド */
public hasChildren(): bool
/* 継承したメソッド */
public ArrayIterator::__construct(array|object $array = [], int $flags = 0)
public ArrayIterator::seek(int $offset): void
}

定義済み定数

RecursiveArrayIterator フラグ

RecursiveArrayIterator::CHILD_ARRAYS_ONLY

(オブジェクトではなく)配列のみが、再帰的な走査が可能な子を持つと見なす

目次

add a note

User Contributed Notes 4 notes

up
18
c dot 1 at smithies dot org
13 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:

<?php
class RecursiveArrayOnlyIterator extends RecursiveArrayIterator {
public function
hasChildren() {
return
is_array($this->current());
}
}
?>
Of course, this simple example will not recurse into ArrayObjects either!
up
16
mccarthy dot richard at gmail dot com
13 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!

<?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
up
8
lemoinem dot remove at me dot mlemoine dot name
11 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 (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)
up
4
Edgar
2 years ago
<?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
*/

?>
To Top