|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77433 iterator_to_array incorrect result when using yield from
Submitted: 2019-01-09 14:00 UTC Modified: 2019-01-09 14:10 UTC
From: jwurtz at jolicode dot com Assigned:
Status: Duplicate Package: Arrays related
PHP Version: 7.1.25 OS:
Private report: No CVE-ID: None
 [2019-01-09 14:00 UTC] jwurtz at jolicode dot com
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:

function yield1() {
    yield 'foo';

function yield2() {
    yield 'bar';
    yield from yield1();

// Will only display foo

// will correctly display each value
foreach (yield2() as $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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-09 14:09 UTC]
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:10 UTC]
-Status: Open +Status: Duplicate
 [2019-01-09 14:10 UTC]
Duplicate of #72518
 [2019-01-09 14:41 UTC] jwurtz at jolicode dot com
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,
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 25 16:01:28 2024 UTC