php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46958 iterator_to_array() does not return every inner iterator of an AppendIterator
Submitted: 2008-12-28 04:12 UTC Modified: 2008-12-29 18:55 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:2 (50.0%)
From: pestilence669@php.net Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.2.8 OS: Mac OS X 10.5.6 PPC
Private report: No CVE-ID: None
 [2008-12-28 04:12 UTC] pestilence669@php.net
Description:
------------
The SPL iterator_to_array() function does not appear to include every 
inner iterator contained within an AppendIterator. It seems that it only 
processes the very last iterator that was appended.

I'd expect that array conversion would parallel the traversal of 
foreach, but it does not. To maintain consistency within SPL itself, I'd 
suggest that this be corrected.

If the behavior can be confirmed, I'm willing to supply the patch & 
regression tests. I'd like more karma.

I've also verified this w/ version 5.2.6 on the same platform and 
version 5.2.3-1ubuntu6 on x86 32-bit Linux.

Reproduce code:
---------------
<?php
$r = new AppendIterator();
$r->append(new ArrayIterator(array(1,2)));
$r->append(new ArrayIterator(array(3,4)));
$r->append(new ArrayIterator(array(5,6)));

// only sees 5 and 6:
print_r(iterator_to_array($r));

// this works as expected: foreach ($r as $i) echo $i;
?>



Expected result:
----------------
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)






Actual result:
--------------
Array
(
    [0] => 5
    [1] => 6
)






Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-29 02:44 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Your keys are conflicting, so the second Iterator is overwriting the keys of the first iterator, then the 3rd overwrites hem. Use the secnd parameter for iterator_to_array()
 [2008-12-29 18:53 UTC] pestilence669@php.net
Still doesn't work as expected:

php -r '$a = new AppendIterator(); $a->append(new ArrayIterator(array(1,2))); $a->append(new ArrayIterator(array(3,4))); print_r(iterator_to_array($a), false);'
Array
(
    [0] => 3
    [1] => 4
)

 [2008-12-29 18:55 UTC] pestilence669@php.net
Bad paren. My bad. Works.

php -r '$a = new AppendIterator(); $a->append(new ArrayIterator(array(1,2))); $a->append(new ArrayIterator(array(3,4))); print_r(iterator_to_array($a, false));'
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 15:01:56 2024 UTC