function.array.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 197 行

HTML
197
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Create an array</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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> &mdash; <span class="dc-title">Create an array</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">array</span> <span class="methodname"><b><b>array</b></b></span>    ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$...</tt></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">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">...</tt></i></span>     <dd>      <p class="para">       Syntax &quot;index =&gt; values&quot;, 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">  <h3 class="title">Return Values</h3>  <p class="para">   Returns an array of the parameters.  The parameters can be given   an index with the <i>=&gt;</i> 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">  <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">    <p><b>Example #1 <b>array()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"fruits"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"numbers"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"holes"&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"first"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"second"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"third"</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 Automatic index with <b>array()</b></b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">19</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</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">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>Array(    [0] =&gt; 1    [1] =&gt; 1    [2] =&gt; 1    [3] =&gt; 13    [4] =&gt; 1    [8] =&gt; 1    [9] =&gt; 19)</pre></div>    </pre></div>   </div>  </p>  <p class="para">   Note that index &#039;3&#039; 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">    <p><b>Example #3 1-based index with <b>array()</b></b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$firstquarter&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'January'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'February'</span><span style="color: #007700">,&nbsp;</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">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>Array(    [1] =&gt; January    [2] =&gt; February    [3] =&gt; March)</pre></div>    </pre></div>   </div>  </p>  <p class="para">   As in Perl, you can access a value from the array inside double quotes.   However, with PHP you&#039;ll need to enclose your array between curly braces.   <div class="example">    <p><b>Example #4 Accessing an array inside double quotes</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;{$foo['bar']}!"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;baz!<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <p class="para">   <blockquote><p><b class="note">Note</b>:          <b>array()</b> is a language construct used to     represent literal arrays, and not a regular function.    <br />   </p></blockquote>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.array-pad.html" class="function" rel="rdfs-seeAlso">array_pad()</a></li>    <li class="member"><a href="function.list.html" class="function" rel="rdfs-seeAlso">list()</a></li>    <li class="member"><a href="function.count.html" class="function" rel="rdfs-seeAlso">count()</a></li>    <li class="member"><a href="function.range.html" class="function" rel="rdfs-seeAlso">range()</a></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 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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?