php教程

The ParentIterator class

(PHP 5 >= 5.1.0, PHP 7)

简介

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

类摘要

ParentIterator extends RecursiveFilterIterator implements RecursiveIterator , OuterIterator {
/* 方法 */
public accept ( void ) : bool
public __construct ( RecursiveIterator $iterator )
public getChildren ( void ) : ParentIterator
public hasChildren ( void ) : bool
public next ( void ) : void
public rewind ( void ) : void
/* 继承的方法 */
}

Table of Contents

User Contributed Notes

dn dot permyakov at gmail dot com 12-May-2019 11:24
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
Anonymous 19-Dec-2011 07:55
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);
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788