Home >Backend Development >PHP Tutorial >Detailed explanation of PHP array array() syntax definition_PHP tutorial

Detailed explanation of PHP array array() syntax definition_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:27:17966browse

After a long period of development of PHP, many users are very familiar with PHP. Here I would like to share my personal understanding and discuss it with you. A PHP array is actually an ordered graph. A graph is a type that maps values ​​to keys.

This type has been optimized in many aspects, so you can use it as a real array, or list (vector), hash table (picture ), dictionaries, sets, stacks, queues and many more possibilities. Since you can use another PHP array as a value, you can also simulate a tree easily. It is beyond the scope of this manual to explain these structures, but you will find at least one example of each structure. For more information on these structures, we recommend that you consult external works on this broad topic. Below we will introduce the relevant knowledge about PHP arrays in detail.

PHP array syntax definition array()

You can use the array() language structure to create a new array. It accepts a certain number of key=>value parameter pairs separated by commas.

<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></SPAN><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>></span></font></strong><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 can be integer or string. If the key name is an integer, it is interpreted as an integer (e.g. "8" will be interpreted as 8, and "08" will be interpreted as "08"). The variable type of array subscript in PHP will not affect the array. There is only one type of array. It can contain both integer and string subscripts, and the value can be any value.

<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 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>></span></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>

If no key is specified for the given value, the current largest integer index value is taken, and the new key name will be that value plus one. If the key name you specify already has a value, the value will be overwritten.

<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 class=""><SPAN>//Thisarrayisthesameas...  </SPAN><LI class=alt><SPAN>array(</SPAN><SPAN class=attribute><FONT color=#ff0000>5</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446538.htmlTechArticleAfter a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and share it with you. Discussion Discuss that a PHP array is actually an ordered graph. The picture is a kind of val...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn