update page now
PHP 8.1.34 Released!

The ParentIterator class

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

Introduzione

This extended FilterIterator allows a recursive iteration using RecursiveIteratorIterator that only shows those elements which have children.

Indice dei contenuti

add a note

User Contributed Notes 2 notes

up
3
dn dot permyakov at gmail dot com
6 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
up
3
Anonymous
14 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);
?>
To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant