Reputation: 7165
When recursively iterating using an instance of \RecursiveIteratorIterator
is it expected behaviour that in looking for a sub iterator of a depth greater than your current depth, it always returns null
?
Please see: RecursiveIteratorIterator::getSubIterator
public RecursiveIterator RecursiveIteratorIterator::getSubIterator ([ int $level ] )
Example:
$innerIterator = new \RecursiveArrayIterator([/*a recursive array*/]);
$iterator = new \RecursiveIteratorIterator($innerIterator);
foreach ($iterator as $value) {
$depth = $iterator->getDepth();
$parentDepth = $depth - 1;
$childDepth = $depth + 1;
// returns \RecursiveArrayIterator
$iterator->getSubIterator($depth);
// returns null first iteration and \RecursiveArrayIterator thereafter
$iterator->getSubIterator($parentDepth);
// always returns null...should it?
$iterator->getSubIterator($childDepth);
}
Upvotes: 1
Views: 681
Reputation: 51950
The RecursiveIteratorIterator::getSubIterator()
method's source code is short enough to be posted here, and is as good a place as any to find an answer.
/* {{{ proto RecursiveIterator RecursiveIteratorIterator::getSubIterator([int level])
The current active sub iterator or the iterator at specified level */
SPL_METHOD(RecursiveIteratorIterator, getSubIterator)
{
spl_recursive_it_object *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
long level = object->level;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &level) == FAILURE) {
return;
}
if (level < 0 || level > object->level) {
RETURN_NULL();
}
RETURN_ZVAL(object->iterators[level].zobject, 1, 0);
} /* }}} */
— From the PHP 5.5.14 source.
The key part for this question is the if
statement:
if (level < 0 || level > object->level) {
RETURN_NULL();
}
The code is pretty self-explanatory, but it is saying:
If the specified level is less than zero, or greater than the current object's level, return null.
Upvotes: 4