Rumah >pembangunan bahagian belakang >tutorial php >有关PHP数组array()语法定义详解_PHP教程
PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解,和大家讨论讨论,PHP数组实际上是一个有序图。图是一种把values映射到keys的类型。
PHP数组语法定义array()
可以用array()语言结构来新建一个array。它接受一定数量用逗号分隔的key=>value参数对。
<ol class="dp-xml"> <li class="alt"><span><span>array([</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>] </span></span></li> <li class=""><span>value </span></li> <li class="alt"><span>,... </span></li> <li class=""><span>) </span></li> <li class="alt"><span>//key可以是integer或者string </span></li> <li class=""><span>//value可以是任何值 </span></li> <li class="alt"><span> </span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">arr</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("foo"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"bar",</span><span class="attribute"><font color="#ff0000">12</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>true); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>echo$arr["foo"];//bar </span></li> <li class=""><span>echo$arr[12];//1 </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
key可以是integer或者string。如果键名是一个integer的标准表达方法,则被解释为整数(例如"8"将被解释为8,而"08"将被解释为"08")。PHP中数组下标的变量类型不会对数组造成影响,数组的类型只有一种,它可以同时包含整型和字符串型的下标,值可以是任何值。
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">arr</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("somearray"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array(</span><span class="attribute"><font color="#ff0000">6</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>5,</span><span class="attribute"><font color="#ff0000">13</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>9,"a"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>42)); </span> </li> <li class="alt"><span>echo$arr["somearray"][6];//5 </span></li> <li class=""><span>echo$arr["somearray"][13];//9 </span></li> <li class="alt"><span>echo$arr["somearray"]["a"];//42 </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
如果对给出的值没有指定键名,则取当前最大的整数索引值,而新的键名将是该值加一。如果你指定的键名已经有了值,则该值会被覆盖。
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>//Thisarrayisthesameas... </span></li> <li class="alt"> <span>array(</span><span class="attribute"><font color="#ff0000">5</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>43,32,56,"b"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>12); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//...thisarray </span></li> <li class=""> <span>array(</span><span class="attribute"><font color="#ff0000">5</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>43,</span><span class="attribute"><font color="#ff0000">6</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>32,</span><span class="attribute"><font color="#ff0000">7</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>56,"b"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>12); </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>