RecursiveCachingIterator::getChildren
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
RecursiveCachingIterator::getChildren — Return the inner iterator's children as a RecursiveCachingIterator
Description
Returns the children of the current element, wrapped in a RecursiveCachingIterator that has the same flags. The children are retrieved from the inner iterator when the iterator moves to the element, so the same object is returned until the iterator moves on.
Parameters
This function has no parameters.
Return Values
The children of the current element, as a
RecursiveCachingIterator;
or null if the current element has no children.
Examples
Example #1 Iterating over children
<?php
$iterator = new RecursiveCachingIterator(
new RecursiveArrayIterator(['a' => 1, 'b' => ['c' => 2, 'd' => 3], 'e' => 4]),
0
);
foreach ($iterator as $key => $value) {
if (!$iterator->hasChildren()) {
echo $key, ' = ', $value, "\n";
continue;
}
echo $key, ":\n";
$children = $iterator->getChildren();
foreach ($children as $childKey => $childValue) {
echo ' ', $childKey, ' = ', $childValue, $children->hasNext() ? '' : ' (last)', "\n";
}
}
?>The above example will output:
a = 1 b: c = 2 d = 3 (last) e = 4
See Also
- RecursiveCachingIterator::hasChildren() - Check whether the current element of the inner iterator has children
- RecursiveIterator::getChildren() - Returns an iterator for the current entry
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
