Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
"yield" overwrites "yield from" items
<?php
function generator1(): \Traversable
{
    yield 'alpha';
    yield 'beta';
    yield 'delta';
}

function generator2(): \Traversable
{
    yield from generator1();
    yield 'upsilon';
    yield 'omega';
}

function resolve(iterable $generator) {
    foreach ($generator as $index => $item) 
        echo "$index $item" . PHP_EOL;
}

resolve(generator1());
echo  PHP_EOL;

resolve(generator2());
die;

Produces this result:

0 alpha
1 beta
2 delta

0 alpha
1 beta
2 delta
0 upsilon
1 omega

That last stream should be:

0 alpha
1 beta
2 delta
3 upsilon
4 omega

What's going on here? Is this a bug in PHP (I'm running v7.4.10 in Ubuntu.) How do I get the correct result without using an array or some other form of collector?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The result is a specialty of the iterator_to_array() function. The second parameter must be set to false so that all values are recorded as already noted in the comment.

This is also specifically pointed out in the PHP manual.

function generator1(): \Traversable
{
    yield 'alpha';
    yield 'beta';
    yield 'delta';
    yield 'gamma';
    yield 'epsilon';
    yield 'eta';
}

function generator2(): \Traversable
{
    yield from generator1();
    yield 'upsilon';
    yield 'omega';
}

var_dump(iterator_to_array(generator2()));

var_dump(iterator_to_array(generator2(), false));

Result:

array(6) {
  [0]=>
  string(7) "upsilon"
  [1]=>
  string(5) "omega"
  [2]=>
  string(5) "delta"
  [3]=>
  string(5) "gamma"
  [4]=>
  string(7) "epsilon"
  [5]=>
  string(3) "eta"
}
array(8) {
  [0]=>
  string(5) "alpha"
  [1]=>
  string(4) "beta"
  [2]=>
  string(5) "delta"
  [3]=>
  string(5) "gamma"
  [4]=>
  string(7) "epsilon"
  [5]=>
  string(3) "eta"
  [6]=>
  string(7) "upsilon"
  [7]=>
  string(5) "omega"
}

Try for yourself in the sandbox.

This becomes clear when keys and values are output with foreach.

foreach(generator2() as $key => $value){
  var_dump($key,$value);
}

Output:

int(0) string(5) "alpha"
int(1) string(4) "beta"
int(2) string(5) "delta"
int(3) string(5) "gamma"
int(4) string(7) "epsilon"
int(5) string(3) "eta"
int(0) string(7) "upsilon"
int(1) string(5) "omega"
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!