The ParentIterator class
(PHP 5 >= 5.1.0)
Introduction
This extended FilterIterator allows a recursive iteration using RecursiveIteratorIterator that only shows those elements which have children.
Class synopsis
/* Methods */
}Table of Contents
- ParentIterator::accept — Determines acceptability
- ParentIterator::__construct — Constructs a ParentIterator
- ParentIterator::getChildren — Return the inner iterator's children contained in a ParentIterator
- ParentIterator::hasChildren — Check whether the inner iterator's current element has children
- ParentIterator::next — Move the iterator forward
- ParentIterator::rewind — Rewind the iterator
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Стандартная библиотека PHP (SPL)
- Класс AppendIterator
- Класс ArrayIterator
- Класс CachingIterator
- Класс CallbackFilterIterator
- Класс DirectoryIterator
- Класс EmptyIterator
- Класс FilesystemIterator
- Класс FilterIterator
- Класс GlobIterator
- Класс InfiniteIterator
- Класс IteratorIterator
- Класс LimitIterator
- Класс MultipleIterator
- Класс NoRewindIterator
- Класс ParentIterator
- Класс RecursiveArrayIterator
- Класс RecursiveCachingIterator
- Класс RecursiveCallbackFilterIterator
- Класс RecursiveDirectoryIterator
- Класс RecursiveFilterIterator
- Класс RecursiveIteratorIterator
- Класс RecursiveRegexIterator
- Класс RecursiveTreeIterator
- Класс RegexIterator
Коментарии
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);
?>
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