RecursiveCachingIterator::getChildren

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

RecursiveCachingIterator::getChildren — Return the inner iterator's children as a RecursiveCachingIterator

Description

public function RecursiveCachingIterator::getChildren(): ?RecursiveCachingIterator

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