Search code examples
phpgenerator

How to yield value with the key?


How to set the key for yielded value? Can the generator have the key as well as array does?

I can easily name keys when returning arrays. It's very useful for PhpUnit dataProviders:

$array = [
    'key' => ['value',1,2,3],
    'here' => ['a',4,5,6],
    'there' => ['b',7,8,9],
];

foreach( $array as $key => $value ){
    echo $key."\t=>\t".var_export($value, true)."\n\n\n";
}

but can I do the same using generators?

e.g., how to change the following code?:

function hi()
{
    yield ['value',1,2,3];
    yield ['a',4,5,6];
    yield ['b',7,8,9];
}
$array = hi();
foreach( $array as $key => $value ){
    echo $key."\t=>\t".var_export($value, true)."\n\n\n";
}

Currently, the output is:

0   =>  array (
  0 => 'value',
  1 => 1,
  2 => 2,
  3 => 3,
)


1   =>  array (
  0 => 'a',
  1 => 4,
  2 => 5,
  3 => 6,
)


2   =>  array (
  0 => 'b',
  1 => 7,
  2 => 8,
  3 => 9,
)

How can I set the mindful keys for yielded values?


Solution

  • It is easy, yield has documented ability to use keys: https://www.php.net/manual/en/language.generators.syntax.php

    function hi()
    {
        yield 'key' => ['value',1,2,3];
        yield 'here' => ['a',4,5,6];
        yield 'there' => ['b',7,8,9];
    }
    $array = hi();
    foreach( $array as $key => $value ){
        echo $key."\t=>\t".var_export($value, true)."\n\n\n";
    }
    

    The output will have keys, as well as an array does:

    key =>  [
      0 => 'value',
      1 => 1,
      2 => 2,
      3 => 3,
    ]
    
    
    here    =>  [
      0 => 'a',
      1 => 4,
      2 => 5,
      3 => 6,
    ]
    
    
    there   =>  [
      0 => 'b',
      1 => 7,
      2 => 8,
      3 => 9,
    ]