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.
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>