📄 language.types.array.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Arrays</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="language.types.string.html">Strings</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.object.html">Objects</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="language.types.array" class="sect1"> <h2 class="title">Arrays</h2> <p class="para"> An <a href="language.types.array.html" class="type array">array</a> in PHP is actually an ordered map. A map is a type that associates <em class="emphasis">values</em> to <em class="emphasis">keys</em>. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As <a href="language.types.array.html" class="type array">array</a> values can be other <a href="language.types.array.html" class="type array">array</a>s, trees and multidimensional <a href="language.types.array.html" class="type array">array</a>s are also possible. </p> <p class="para"> Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic. </p> <div id="language.types.array.syntax" class="sect2"> <h3 class="title">Syntax</h3> <div id="language.types.array.syntax.array-func" class="sect3"> <h4 class="title">Specifying with <a href="function.array.html" class="function">array()</a></h4> <p class="para"> An <a href="language.types.array.html" class="type array">array</a> can be created by the <a href="function.array.html" class="function">array()</a> language construct. It takes as parameters any number of comma-separated <i><span class="replaceable">key</span> => <span class="replaceable">value</span></i> pairs. </p> <pre class="synopsis">array( <span class="optional"> <span class="replaceable">key</span> => </span> <span class="replaceable">value</span> , ... )// <span class="replaceable">key</span> may only be an <a href="language.types.integer.html" class="type integer">integer</a> or <a href="language.types.string.html" class="type string">string</a>// <span class="replaceable">value</span> may be any value of any type</pre> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">, </span><span style="color: #0000BB">12 </span><span style="color: #007700">=> </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">]; </span><span style="color: #FF8000">// bar<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">12</span><span style="color: #007700">]; </span><span style="color: #FF8000">// 1<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> A <var class="varname"><a href="function.key.html" class="classname">key</a></var> may be either an <a href="language.types.integer.html" class="type integer">integer</a> or a <a href="language.types.string.html" class="type string">string</a>. If a key is the standard representation of an <a href="language.types.integer.html" class="type integer">integer</a>, it will be interpreted as such (i.e. <i>"8"</i> will be interpreted as <i>8</i>, while <i>"08"</i> will be interpreted as <i>"08"</i>). <a href="language.types.float.html" class="type Float">Float</a>s in <var class="varname"><a href="function.key.html" class="classname">key</a></var> are truncated to <a href="language.types.integer.html" class="type integer">integer</a>. The indexed and associative <a href="language.types.array.html" class="type array">array</a> types are the same type in PHP, which can both contain <a href="language.types.integer.html" class="type integer">integer</a> and <a href="language.types.string.html" class="type string">string</a> indices. </p> <p class="para"> A value can be any PHP type. </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"somearray" </span><span style="color: #007700">=> array(</span><span style="color: #0000BB">6 </span><span style="color: #007700">=> </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">13 </span><span style="color: #007700">=> </span><span style="color: #0000BB">9</span><span style="color: #007700">, </span><span style="color: #DD0000">"a" </span><span style="color: #007700">=> </span><span style="color: #0000BB">42</span><span style="color: #007700">));<br /><br />echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"somearray"</span><span style="color: #007700">][</span><span style="color: #0000BB">6</span><span style="color: #007700">]; </span><span style="color: #FF8000">// 5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"somearray"</span><span style="color: #007700">][</span><span style="color: #0000BB">13</span><span style="color: #007700">]; </span><span style="color: #FF8000">// 9<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"somearray"</span><span style="color: #007700">][</span><span style="color: #DD0000">"a"</span><span style="color: #007700">]; </span><span style="color: #FF8000">// 42<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> If a key is not specified for a value, the maximum of the <a href="language.types.integer.html" class="type integer">integer</a> indices is taken and the new key will be that value plus 1. If a key that already has an assigned value is specified, that value will be overwritten. </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// This array is the same as ...<br /></span><span style="color: #007700">array(</span><span style="color: #0000BB">5 </span><span style="color: #007700">=> </span><span style="color: #0000BB">43</span><span style="color: #007700">, </span><span style="color: #0000BB">32</span><span style="color: #007700">, </span><span style="color: #0000BB">56</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=> </span><span style="color: #0000BB">12</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// ...this array<br /></span><span style="color: #007700">array(</span><span style="color: #0000BB">5 </span><span style="color: #007700">=> </span><span style="color: #0000BB">43</span><span style="color: #007700">, </span><span style="color: #0000BB">6 </span><span style="color: #007700">=> </span><span style="color: #0000BB">32</span><span style="color: #007700">, </span><span style="color: #0000BB">7 </span><span style="color: #007700">=> </span><span style="color: #0000BB">56</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=> </span><span style="color: #0000BB">12</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="warning"><b class="warning">Warning</b> <p class="simpara"> Before PHP 4.3.0, appending to an <a href="language.types.array.html" class="type array">array</a> in which the current maximum key was negative would create a new key as described above. Since PHP 4.3.0, the new key will be <i>0</i>. </p> </div> <p class="para"> Using <b><tt>TRUE</tt></b> as <var class="varname"><a href="function.key.html" class="classname">key</a></var> will evaluate to <a href="language.types.integer.html" class="type integer">integer</a> <i>1</i> as a key. Using <b><tt>FALSE</tt></b> as <var class="varname"><a href="function.key.html" class="classname">key</a></var> will evaluate to <a href="language.types.integer.html" class="type integer">integer</a> <i>0</i> as a key. Using <b><tt>NULL</tt></b> as a key will evaluate to the empty string. Using the empty string as a key will create (or overwrite) a key with the empty string and its value; it is <em class="emphasis">not</em> the same as using empty brackets. </p> <p class="para"> <a href="language.types.array.html" class="type Array">Array</a>s and <a href="language.types.object.html" class="type object">object</a>s can not be used as keys. Doing so will result in a warning: <i>Illegal offset type</i>. </p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -