PHP 8.1.0 Beta 1 available for testing

La clase ParentIterator

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

Introducción

Esta FilterIterator ampliada permite una iteración recursiva usando RecursiveIteratorIterator, que solamente muestra los elementos que tienen hijos.

Sinopsis de la Clase

ParentIterator extends RecursiveFilterIterator implements RecursiveIterator, OuterIterator {
/* Métodos */
public accept(): bool
public __construct(RecursiveIterator $iterator)
public hasChildren(): bool
public next(): void
public rewind(): void
/* Métodos heredados */
}

Tabla de contenidos

add a note add a note

User Contributed Notes 2 notes

up
4
Anonymous
9 years ago
ParentIterator is just a RecursiveFilterIterator whos accept() method calls the RecursiveFilterIterator->hasChildren() method to filter itself.

Basically, it filters out leaf nodes. For example

This would yield all files and directories
<?php
$rdi
= new RecursiveDirectoryIterator(__DIR__);
$iter = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::CHILD_FIRST);
?>

wrapping that in a ParentIterator would filter it down to just directories
<?php
$rdi
= new RecursiveDirectoryIterator(__DIR__);
$iter = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::CHILD_FIRST);
$dirsOnly = new ParentIterator($iter);
?>
up
2
dn dot permyakov at gmail dot com
2 years ago
Easy example for understanding:
<?php

$iterator
= new ParentIterator(
    new
RecursiveArrayIterator(
        array(array(
1, 2, 3), 'A', 'B', 'C')
    )
);

foreach (
$iterator as $recursive) {
    foreach (
$recursive as $value) {
        echo
$value . PHP_EOL;
    }
}
?>

1
2
3
To Top