Home >Backend Development >PHP Tutorial >Learn more about how to use PHP array functions_PHP Tutorial
PHP array functions are quite commonly used, so I studied the PHP array functions and shared them with you here. I hope it will be useful to everyone. Today we are learning about PHP array functions. Arrays are a data type that are used very frequently. Learning how to handle arrays will make it easier for you to build a website.
1.array_multisort() to sort multiple arrays or multi-dimensional arrays can be used to sort multiple arrays at one time or to sort multi-dimensional arrays according to a certain dimension. The original key name association is retained during sorting, and the function implemented is similar to ORDERBY in SQL.
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">ar1</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("10",100,100,"a"); </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">ar2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(1,3,"2",1); </span> </li> <li class="alt"><span>array_multisort($ar1,$ar2); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"><span>//返回结果(保持了原来数组的关联性) </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">ar1</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">Array</font></span><span>([0]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>10[1]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>a[2]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>100[3]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>100) </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">ar2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">Array</font></span><span>([0]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>1[1]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>1[2]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>2[3]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>3) </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//实现学生成绩排名 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">grade</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("score"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array(70,95,70.0,60,"70"), </span> </li> <li class="alt"> <span>"name"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array("ZhangSan","LiSi","WangWu", </span> </li> <li class=""><span>"ZhaoLiu","LiuQi")); </span></li> <li class="alt"><span>array_multisort($grade["score"],SORT_NUMERIC,SORT_DESC, </span></li> <li class=""><span>//将分数作为数值,由高到低排序 </span></li> <li class="alt"><span>$grade["name"],SORT_STRING,SORT_ASC); </span></li> <li class=""><span>//将名字作为字符串,由小到大排序 </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class=""><span>//输出结果 </span></li> <li class="alt"><span> </span></li> <li class=""><span>array( </span></li> <li class="alt"> <span>'score'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class=""><span>array( </span></li> <li class="alt"> <span></span><span class="attribute"><font color="#ff0000">0</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>95, </span> </li> <li class=""> <span></span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'70', </span> </li> <li class="alt"> <span></span><span class="attribute"><font color="#ff0000">2</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>70, </span> </li> <li class=""> <span></span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>70, </span> </li> <li class="alt"> <span></span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>60, </span> </li> <li class=""><span>), </span></li> <li class="alt"> <span>'name'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class=""><span>array( </span></li> <li class="alt"> <span></span><span class="attribute"><font color="#ff0000">0</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'LiSi', </span> </li> <li class=""> <span></span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'LiuQi', </span> </li> <li class="alt"> <span></span><span class="attribute"><font color="#ff0000">2</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'WangWu', </span> </li> <li class=""> <span></span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'ZhangSan', </span> </li> <li class="alt"> <span></span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'ZhaoLiu', </span> </li> <li class=""><span>), </span></li> <li class="alt"><span>) </span></li> </ol>
2.array_rand() and shuffle(), one is to randomly take out one or more values from the array, the other is to disrupt the order of the values in the array, and then you can Retrieve values, they all implement the function of randomly extracting data from the array, and can be used to implement functions such as lottery.
3.array_reduce() iteratively applies the callback function function to each unit in the input array, thereby reducing the array to a single value. If the optional initial parameter is specified, it will be treated as the first value in the array, or as the final return value if the array is empty.
<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>functionrsum($v,$w){ </SPAN><LI class=alt><SPAN>$v+=$w; </SPAN><LI class=""><SPAN>return$v; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>functionrmul($v,$w){ </SPAN><LI class=""><SPAN>$v*=$w; </SPAN><LI class=alt><SPAN>return$v; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>a</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(1,2,3,4,5); </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>x</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(); </SPAN></SPAN><LI class=""><SPAN>//实现array_sum()功能 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>b</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_reduce</FONT></SPAN><SPAN>($a,"rsum");//</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN class=attribute><FONT color=#ff0000>15</FONT></SPAN><SPAN>=1+2+3+4+5 </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>c</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_reduce</FONT></SPAN><SPAN>($a,"rmul",10);//</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN class=attribute><FONT color=#ff0000>1200</FONT></SPAN><SPAN>=1*2*3*4*5*10 </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>d</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_reduce</FONT></SPAN><SPAN>($x,"rsum",1);//1 </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
This function can realize the sum of an array (product, difference, etc. functions)