|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-05-06 15:47 UTC] jani@php.net
[2009-05-14 01:00 UTC] php-bugs at lists dot php dot net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 03:00:02 2025 UTC |
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.' - '; }