php.net |  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
46 - 24 = ?
Subscribe to this entry?

 
 [2019-01-09 14:00 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-09 14:09 UTC] 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:10 UTC] salathe@php.net
-Status: Open +Status: Duplicate
 [2019-01-09 14:10 UTC] salathe@php.net
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: Wed Apr 24 13:01:29 2024 UTC