Bug #77433 [Com]: iterator_to_array incorrect result when using yield from

From: Date: Wed, 09 Jan 2019 14:41:15 +0000
Subject: Bug #77433 [Com]: iterator_to_array incorrect result when using yield from
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-218869@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77433&edit=1 ID: 77433 Comment by: jwurtz at jolicode dot com Reported by: jwurtz at jolicode dot com Summary: iterator_to_array incorrect result when using yield from Status: Duplicate Type: Bug Package: Arrays related PHP Version: 7.1.25 Block user comment: N Private report: N New Comment: Oh, didn't see that, only search on open issue and not closed, sorry for the noise. I made a patch to the documentation that add an exemple for this use case, warning on preserve keys was not so obvious for me, as in my head talking about keys is only relevant for string keys on not numeric ones. Thanks again for the quick answer, Previous Comments: ------------------------------------------------------------------------ [2019-01-09 14:10:32] salathe@php.net Duplicate of #72518 ------------------------------------------------------------------------ [2019-01-09 14:09:17] salathe@php.net Be sure to set the $use_keys parameter of iterator_to_array() to false, since the keys overlap (try foreach and printing the keys). ------------------------------------------------------------------------ [2019-01-09 14:00:30] jwurtz at jolicode dot com Description: ------------ When using iterator_to_array on a function that return a \Generator, if this function use both yield and yield from it will only put values of the yield from expression. Test script: --------------- <?php function yield1() { yield 'foo'; } function yield2() { yield 'bar'; yield from yield1(); } // Will only display foo var_dump(iterator_to_array(yield2())); // will correctly display each value foreach (yield2() as $data) { var_dump($data); } Expected result: ---------------- It should create an array with all values and not only the one from the yield from expression. Actual result: -------------- Create an array only on the yield from expression ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77433&edit=1

« previous php.bugs (#218869) next »