CKTIS / PHP
Keyword yield from
Delegate iteration to an array or Traversable, forwarding its keys and values.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
yield from $iterable;Delegate iteration to an array or Traversable, forwarding its keys and values. Delegating to a generator also makes its return value the result of the yield from expression. Keys are preserved, so converting duplicate keys to an array may overwrite an earlier value.
function inner(){yield "x"=>2;return 7;} function outer(){yield "x"=>1; $n=yield from inner(); return $n;}
$g=outer(); $items=[]; foreach($g as $k=>$v){$items[]=[$k,$v];} echo json_encode([$items,$g->getReturn()]);
[[["x",1],["x",2]],7]
Examples
<?php
function inner(){yield "x"=>2;return 7;} function outer(){yield "x"=>1; $n=yield from inner(); return $n;}
$g=outer(); $items=[]; foreach($g as $k=>$v){$items[]=[$k,$v];} echo json_encode([$items,$g->getReturn()]);
Result
[[["x",1],["x",2]],7]