略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ParentIterator

2024-11-14

The ParentIterator class

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

简介

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

类摘要

class ParentIterator extends RecursiveFilterIterator {
/* 方法 */
public __construct(RecursiveIterator $iterator)
public accept(): bool
public hasChildren(): bool
public next(): void
public rewind(): void
/* 继承的方法 */
public FilterIterator::accept(): bool
public FilterIterator::next(): void
public FilterIterator::rewind(): void
public FilterIterator::valid(): bool
public IteratorIterator::next(): void
}

目录

add a noteadd a note

User Contributed Notes 2 notes

up
4
Anonymous
10 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
3
dn dot permyakov at gmail dot com
3 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

官方地址:https://www.php.net/manual/en/class.parentiterator.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3