svn: /phpdoc/en/trunk/ language/generators.xml reference/spl/functions/iterator-to-array.xml
| From: | Bob Weinand | Date: | Thu, 30 Jun 2016 00:24:35 +0000 |
| Subject: | svn: /phpdoc/en/trunk/ language/generators.xml reference/spl/functions/iterator-to-array.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-14269@lists.php.net to get a copy of this message | ||
bwoebi Thu, 30 Jun 2016 00:24:35 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=339531
Log:
Add caution/note regarding yield from and iterator_to_array
It's been a common source of mistake, see also https://bugs.php.net/72518
Changed paths:
U phpdoc/en/trunk/language/generators.xml
U phpdoc/en/trunk/reference/spl/functions/iterator-to-array.xml
Modified: phpdoc/en/trunk/language/generators.xml
===================================================================
--- phpdoc/en/trunk/language/generators.xml 2016-06-29 19:10:08 UTC (rev 339530)
+++ phpdoc/en/trunk/language/generators.xml 2016-06-30 00:24:35 UTC (rev 339531)
@@ -401,7 +401,62 @@
<command>yield from</command> expression will also return any value
returned by the inner generator.
</para>
+
+ <caution>
+ <title>Storing into an array (e.g. with
<function>iterator_to_array</function>)</title>
+ <para>
+ <command>yield from</command> does not reset the keys. It preserves
+ the keys returned by the <classname>Traversable</classname> object, or
+ <type>array</type>. Thus some values may share a common key with another
+ <command>yield</command> or <command>yield from</command>, which,
upon
+ insertion into an array, will overwrite former values with that key.
+ </para>
+
+ <para>
+ A common case where this matters is <function>iterator_to_array</function>
+ returning a keyed array by default, leading to possibly unexpected results.
+ <function>iterator_to_array</function> has a second parameter
+ <parameter>use_keys</parameter> which can be set to &false; to collect
+ all the values while ignoring the keys returned by the
<classname>Generator</classname>.
+ </para>
+
+ <example>
+ <title><command>yield from</command> with
<function>iterator_to_array</function></title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+function from() {
+ yield 1; // key 0
+ yield 2; // key 1
+ yield 3; // key 2
+}
+function gen() {
+ yield 0; // key 0
+ yield from from(); // keys 0-2
+ yield 4; // key 1
+}
+// pass false as second parameter to get an array [0, 1, 2, 3, 4]
+var_dump(iterator_to_array(gen()));
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+array(3) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(4)
+ [2]=>
+ int(3)
+}
+]]>
+ </screen>
+ </example>
+ </caution>
+
<example>
<title>Basic use of <command>yield from</command></title>
<programlisting role="php">
Modified: phpdoc/en/trunk/reference/spl/functions/iterator-to-array.xml
===================================================================
--- phpdoc/en/trunk/reference/spl/functions/iterator-to-array.xml 2016-06-29 19:10:08 UTC (rev
339530)
+++ phpdoc/en/trunk/reference/spl/functions/iterator-to-array.xml 2016-06-30 00:24:35 UTC (rev
339531)
@@ -45,6 +45,14 @@
their resource ID, and <type>boolean</type> keys will be converted to
integers.
</para>
+ <note>
+ <para>
+ If this parameter is not set or set to &true;, duplicate keys will be
+ overwritten. The last value with a given key will be in the returned
+ <type>array</type>. Set this paramater to &false; to get all the values
+ in any case.
+ </para>
+ </note>
</listitem>
</varlistentry>
</variablelist>