Bug #72518 [Nab]: iterator_to_array() misses values from generator using yield from

From: Date: Thu, 30 Jun 2016 00:27:33 +0000
Subject: Bug #72518 [Nab]: iterator_to_array() misses values from generator using yield from
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201925@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72518&edit=1 ID: 72518 Updated by: bwoebi@php.net Reported by: trowski@php.net Summary: iterator_to_array() misses values from generator using yield from Status: Not a bug Type: Bug Package: SPL related Operating System: OS X PHP Version: 7.0.8 Block user comment: N Private report: N New Comment: For the record, I've added a note to the documentation: http://svn.php.net/viewvc?view=revision&revision=339531 Perhaps the world is more aware of this then... Previous Comments: ------------------------------------------------------------------------ [2016-06-29 21:06:23] trowski@php.net Totally forgot that keys reset in the new generator. Sorry for the noise. ------------------------------------------------------------------------ [2016-06-29 20:50:47] nikic@php.net Your iterator has duplicate keys. Use iterator_to_array() with $preserve_keys=false. ------------------------------------------------------------------------ [2016-06-29 20:36:33] trowski@php.net Description: ------------ iterator_to_array() does not add all values yielded from a generator if yield from is used in the generator. Test script: --------------- <?php function gen1() { yield 1; yield from gen2(); yield 5; } function gen2() { yield 2; yield 3; yield 4; } print_r(iterator_to_array(gen1(), true)); $result = []; foreach (gen1() as $yielded) { $result[] = $yielded; } print_r($result); Expected result: ---------------- Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Actual result: -------------- Array ( [0] => 2 [1] => 5 [2] => 4 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72518&edit=1

« previous php.bugs (#201925) next »