note 129678 added to language.generators.syntax
| From: | harl@gmail.com | Date: | Tue, 30 Jul 2024 23:59:54 +0000 |
| Subject: | note 129678 added to language.generators.syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221451@lists.php.net to get a copy of this message | ||
If you mix yielding values with keys and yielding values without keys, the result is the same as
adding values to an array with or without keys.
<?php
function gen() {
yield 'a';
yield 4 => 'b';
yield 'c';
}
$t = iterator_to_array(gen());
var_dump($t);
?>
The result is an array [0 => 'a', 4 => 'b', 5 => 'c'], just
as if you had written
<?php
$t = [];
$t[] = 'a';
$t[4] = 'b';
$t[] = 'c';
var_dump($t);
?>
With the key given to 'c' being incremented from the previous numeric index.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 125.237.145.229)
----
Manual Page -- https://php.net/manual/en/language.generators.syntax.php
Edit -- https://main.php.net/note/edit/129678
Del: integrated -- https://main.php.net/note/delete/129678/integrated
Del: useless -- https://main.php.net/note/delete/129678/useless
Del: bad code -- https://main.php.net/note/delete/129678/bad+code
Del: spam -- https://main.php.net/note/delete/129678/spam
Del: non-english -- https://main.php.net/note/delete/129678/non-english
Del: in docs -- https://main.php.net/note/delete/129678/in+docs
Del: other reasons-- https://main.php.net/note/delete/129678
Reject -- https://main.php.net/note/reject/129678
Search -- https://main.php.net/manage/user-notes.php