<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Copy the iterator into an array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.iterator-count.html">iterator_count</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.spl-autoload-call.html">spl_autoload_call</a></div> <div class="up"><a href="ref.spl.html">SPL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.iterator-to-array" class="refentry"> <div class="refnamediv"> <h1 class="refname">iterator_to_array</h1> <p class="verinfo">(PHP 5 >= 5.1.0)</p><p class="refpurpose"><span class="refname">iterator_to_array</span> — <span class="dc-title">Copy the iterator into an array</span></p> </div> <div class="refsect1 description" id="refsect1-function.iterator-to-array-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>iterator_to_array</strong></span> ( <span class="methodparam"><span class="type"><a href="class.traversable.html" class="type Traversable">Traversable</a></span> <code class="parameter">$iterator</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_keys</code><span class="initializer"> = true</span></span> ] )</div> <p class="para rdfs-comment"> Copy the elements of an iterator into an array. </p> </div> <div class="refsect1 parameters" id="refsect1-function.iterator-to-array-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">iterator</code></em></span> <dd> <p class="para"> The iterator being copied. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">use_keys</code></em></span> <dd> <p class="para"> Whether to use the iterator element keys as index. </p> <p class="para"> In PHP 5.5 and later, if a key is an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> or <span class="type"><a href="language.types.object.html" class="type object">object</a></span>, a warning will be generated. <strong><code>NULL</code></strong> keys will be converted to an empty string, <span class="type"><a href="language.types.float.html" class="type double">double</a></span> keys will be truncated to their <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> counterpart, <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> keys will generate a warning and be converted to their resource ID, and <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> keys will be converted to integers. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.iterator-to-array-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> An <span class="type"><a href="language.types.array.html" class="type array">array</a></span> containing the elements of the <em><code class="parameter">iterator</code></em>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.iterator-to-array-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.5.0</td> <td> <span class="function"><strong>iterator_to_array()</strong></span> gained support for key types other than <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> and <span class="type"><a href="language.types.string.html" class="type string">string</a></span> when the <em><code class="parameter">use_keys</code></em> parameter is enabled. </td> </tr> <tr> <td>5.2.1</td> <td> The <em><code class="parameter">use_keys</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.iterator-to-array-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3917"> <p><strong>Example #1 <span class="function"><strong>iterator_to_array()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$iterator </span><span style="color: #007700">= new </span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'recipe'</span><span style="color: #007700">=></span><span style="color: #DD0000">'pancakes'</span><span style="color: #007700">, </span><span style="color: #DD0000">'egg'</span><span style="color: #007700">, </span><span style="color: #DD0000">'milk'</span><span style="color: #007700">, </span><span style="color: #DD0000">'flour'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">iterator_to_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">iterator_to_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(4) { ["recipe"]=> string(8) "pancakes" [0]=> string(3) "egg" [1]=> string(4) "milk" [2]=> string(5) "flour" } array(4) { [0]=> string(8) "pancakes" [1]=> string(3) "egg" [2]=> string(4) "milk" [3]=> string(5) "flour" } </pre></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.iterator-count.html">iterator_count</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.spl-autoload-call.html">spl_autoload_call</a></div> <div class="up"><a href="ref.spl.html">SPL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>