Description:
------------
It is impossible to iterate through the children of a tree structure with a RecursiveRegexIterator combined with a RecursiveIteratorIterator.
It only works when using the regex /A/, /r/ or /y/, because this characters are in the word "Array".
See line 74 of ext/spl/internal/regexiterator.inc:
$subject = ($this->flags & self::USE_KEY) ? $this->key : $this->current;
When iterating over an array of arrays (e.g. array[3] in the above example) "$subject" gets the value of "$this->current". In the case of an arraym this value will be "Array".
This explains, why in the above example the regex "/y/" works, while "/v/" is not. "/y/" finds a match in "Array".
The methode "accept()" of the RegexIterator-Object should first check, whether an array has children, when working on nested arrays. If a nested array is found, there is no sense in matching the regex against "Array".
Possible workaround could be adding the following lines above line 74 in ext/spl/internal/regexiterator.inc:
if (is_array($subject) == true) {
return true;
}
Reproduce code:
---------------
$array = array();
$array[0] = 'one';
$array[1] = 'two';
$array[2] = 'three';
$array[3] = array('four', 'five', 'xxx', 'yyy');
$array[4] = 'six';
$array[5] = 'seven';
// Correct output:
// "yyy - "
$recArrayIt = new RecursiveArrayIterator($array);
$recRegexIt = new RecursiveRegexIterator($recArrayIt, '/y/');
$recItIt = new RecursiveIteratorIterator($recRegexIt);
foreach($recItIt as $element) {
echo $element.' - ';
}
// Wrong output: "seven - "
// Should be : "five - seven -"
$recArrayIt = new RecursiveArrayIterator($array);
$recRegexIt = new RecursiveRegexIterator($recArrayIt, '/v/');
$recItIt = new RecursiveIteratorIterator($recRegexIt);
foreach($recItIt as $element) {
echo $element.' - ';
}

Patches

Pull Requests

History

What exactly is the expected and what is actual output?
Also, reproduce scripts should be as short as possible, start with <?php
and end with ?>

[2009-05-14 01:00 UTC] php-bugs at lists dot php dot net

No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".