CKTIS / PHP
Parola chiave yield from
Delega l’iterazione a un array o Traversable inoltrando chiavi e valori.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
yield from $iterable;Delega l’iterazione a un array o Traversable inoltrando chiavi e valori. Con un generatore, il suo ritorno diventa il risultato di yield from. Le chiavi sono conservate, quindi nella conversione in array un duplicato può sovrascrivere un valore precedente.
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]
Esempi
<?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()]);
Risultato
[[["x",1],["x",2]],7]