Home > Article > Backend Development > Case Study PHP Array Query_PHP Tutorial
When learning PHP arrays, you may encounter problems. Here we will introduce PHP array queries and share them with you here. There are more than 30 new array-related functions in PHP4.0. Many of these general functions allow you to check whether a specific object exists in a given array, count array elements, add or remove elements, or sort elements.
PHP array query code:
<ol class="dp-xml"> <li class="alt"><span><span>/** </span></span></li> <li class=""><span>*倒序数组 </span></li> <li class="alt"><span>*@paramaInputarray要排序的数组 </span></li> <li class=""><span>*@param$bystring排序对象,key为按数组下标排序,value为按值排序 </span></li> <li class="alt"><span>*/ </span></li> <li class=""> <span>functionaryReverts($aInput,$</span><span class="attribute"><font color="#ff0000">by</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'key'</font></span><span>){ </span> </li> <li class="alt"><span>if(!is_array($aInput)){ </span></li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">res</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">n</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($aInput); </span> </li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>$n;$i++){ </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tmMax</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>getMax</FONT></SPAN><SPAN>($aInput,$by); </SPAN></SPAN><LI class=""><SPAN>print_r($tmMax); </SPAN><LI class=alt><SPAN>list($k,$v)=each($tmMax); </SPAN><LI class=""><SPAN>$res[$k]=$v; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>return$res; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>/** </SPAN><LI class=alt><SPAN>*@paramaInputarray要冒沟的数组 </SPAN><LI class=""><SPAN>*@param$bystring排序对象,key为按数组下标排序,value为按值排序 </SPAN><LI class=alt><SPAN>*/ </SPAN><LI class=""><SPAN>functiongetMax(&$aInput,$by){ </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>max</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>maxKey</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>if($</SPAN><SPAN class=attribute><FONT color=#ff0000>by</FONT></SPAN><SPAN>=='key') </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tar</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'k'</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>else </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tar</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'v'</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>foreach($aInputas$</SPAN><SPAN class=attribute><FONT color=#ff0000>k</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span>$v){ </span> </li> <li class=""> <span>if($$tar</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$max){ </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">max</font></span><span>=$$tar; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">maxKey</font></span><span>=$k; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">maxValue</font></span><span>=$aInput[$maxKey]; </span> </li> <li class=""><span>unset($aInput[$maxKey]); </span></li> <li class="alt"> <span>returnarray($</span><span class="attribute"><font color="#ff0000">maxKey</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$maxValue); </span> </li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">list</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('apple'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>300,'orange'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>150,'banana'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>100,'mango'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>330); </span> </li> <li class=""> <span>echo'</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>xmp</SPAN><SPAN class=tag>></span></font></strong><span>'; </span> </li> <li class="alt"><span>print_r(aryReverts($list,'value')); </span></li> </ol>