<!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>Create an array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-walk.html">array_walk</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.arsort.html">arsort</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.array" class="refentry"> <div class="refnamediv"> <h1 class="refname">array</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">array</span> — <span class="dc-title">Create an array</span></p> </div> <div class="refsect1 description" id="refsect1-function.array-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>array</strong></span> ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span> ] )</div> <p class="para rdfs-comment"> Creates an array. Read the section on the <a href="language.types.array.html" class="link">array type</a> for more information on what an array is. </p> </div> <div class="refsect1 parameters" id="refsect1-function.array-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">...</code></em></span> <dd> <p class="para"> Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1. Note that when two identical index are defined, the last overwrite the first. </p> <p class="para"> Having a trailing comma after the last defined array entry, while unusual, is a valid syntax. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.array-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array of the parameters. The parameters can be given an index with the <em>=></em> operator. Read the section on the <a href="language.types.array.html" class="link">array type</a> for more information on what an array is. </p> </div> <div class="refsect1 examples" id="refsect1-function.array-examples"> <h3 class="title">Examples</h3> <p class="para"> The following example demonstrates how to create a two-dimensional array, how to specify keys for associative arrays, and how to skip-and-continue numeric indices in normal arrays. <div class="example" id="example-4999"> <p><strong>Example #1 <span class="function"><strong>array()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$fruits </span><span style="color: #007700">= array (<br /> </span><span style="color: #DD0000">"fruits" </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"orange"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">, </span><span style="color: #DD0000">"c" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"apple"</span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">"numbers" </span><span style="color: #007700">=> array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">"holes" </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">"first"</span><span style="color: #007700">, </span><span style="color: #0000BB">5 </span><span style="color: #007700">=> </span><span style="color: #DD0000">"second"</span><span style="color: #007700">, </span><span style="color: #DD0000">"third"</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5000"> <p><strong>Example #2 Automatic index with <span class="function"><strong>array()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$array </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">8 </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">4 </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">19</span><span style="color: #007700">, </span><span style="color: #0000BB">3 </span><span style="color: #007700">=> </span><span style="color: #0000BB">13</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</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 ( [0] => 1 [1] => 1 [2] => 1 [3] => 13 [4] => 1 [8] => 1 [9] => 19 ) </pre></div> </div> </div> </p> <p class="para"> Note that index '3' is defined twice, and keep its final value of 13. Index 4 is defined after index 8, and next generated index (value 19) is 9, since biggest index was 8. </p> <p class="para"> This example creates a 1-based array. <div class="example" id="example-5001"> <p><strong>Example #3 1-based index with <span class="function"><strong>array()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$firstquarter </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1 </span><span style="color: #007700">=> </span><span style="color: #DD0000">'January'</span><span style="color: #007700">, </span><span style="color: #DD0000">'February'</span><span style="color: #007700">, </span><span style="color: #DD0000">'March'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstquarter</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 ( [1] => January [2] => February [3] => March ) </pre></div> </div> </div> </p> <p class="para"> As in Perl, you can access a value from the array inside double quotes. However, with PHP you'll need to enclose your array between curly braces. <div class="example" id="example-5002"> <p><strong>Example #4 Accessing an array inside double quotes</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$foo </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'bar' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Hello </span><span style="color: #007700">{</span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">]}</span><span style="color: #DD0000">!"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello baz!<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.array-notes"> <h3 class="title">Notes</h3> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> <span class="function"><strong>array()</strong></span> is a language construct used to represent literal arrays, and not a regular function. </p> </p></blockquote> </p> </div> <div class="refsect1 seealso" id="refsect1-function.array-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.array-pad.html" class="function" rel="rdfs-seeAlso">array_pad()</a> - Pad array to the specified length with a value</span></li> <li class="member"> <span class="function"><a href="function.list.html" class="function" rel="rdfs-seeAlso">list()</a> - Assign variables as if they were an array</span></li> <li class="member"> <span class="function"><a href="function.count.html" class="function" rel="rdfs-seeAlso">count()</a> - Count all elements in an array, or something in an object</span></li> <li class="member"> <span class="function"><a href="function.range.html" class="function" rel="rdfs-seeAlso">range()</a> - Create an array containing a range of elements</span></li> <li class="member"><a href="control-structures.foreach.html" class="link">foreach</a></li> <li class="member">The <a href="language.types.array.html" class="link">array</a> type</li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-walk.html">array_walk</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.arsort.html">arsort</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>