PHP arrays are one-dimensional arrays and non-arrays. Here we analyze the techniques of sorting PHP two-dimensional arrays. PHP, a nested abbreviation name, is the abbreviation of English Hypertext Preprocessing Language (PHP: Hypertext Preprocessor). PHP is an HTML embedded language. It is a scripting language that is executed on the server side and embedded in HTML documents. The style of the language is similar to C language. It is now widely used by many website programmers. PHP's unique syntax is a mix of C, Java, Perl, and PHP's own innovative syntax.
PHP two-dimensional array sorting uses the asort function, but if it is a multi-dimensional array, it is sorted according to one of the index values. Generally speaking, this type of sorting will introduce temporary arrays to store intermediate variables such as key values to achieve a sorting of multi-dimensional arrays by sorting keys. The following is a piece of code I wrote to sort according to the time value in the two-dimensional array:
PHP two-dimensional array sorting code:
<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>//以时间InputDate为key值进行二维数组降序排列 </SPAN><LI class=alt><SPAN> function array_sort($arr,$keys,$type){ </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>keysvalue</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(); </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>i</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>foreach($arr as $</SPAN><SPAN class=attribute><FONT color=#ff0000>key</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>$val) { </span> </li> <li class="alt"><span>$val[$keys] = str_replace("-","",$val[$keys]); </span></li> <li class=""><span>$val[$keys] = str_replace(" ","",$val[$keys]); </span></li> <li class="alt"><span>$val[$keys] = str_replace(":","",$val[$keys]); </span></li> <li class=""><span>$keysvalue[] =$val[$keys]; </span></li> <li class="alt"><span> </span></li> <li class=""><span>} </span></li> <li class="alt"><span>asort($keysvalue); //key值排序 </span></li> <li class=""><span>reset($keysvalue); //指针重新指向数组第一个 </span></li> <li class="alt"> <span>foreach($keysvalue as $</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$vals) { </span> </li> <li class=""><span>$keysort[] = $key; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">new_array</font></span><span> = array(); </span> </li> <li class="alt"><span>if($type != "asc"){ </span></li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">ii</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($keysort)-1; $ii</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>=0; $ii--) { </span> </li> <li class="alt"><span>$new_array[] = $arr[$keysort[$ii]]; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>}else{ </span></li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">ii</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $ii</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>count</SPAN></FONT></STRONG><SPAN>($keysort); $ii++){ </SPAN></SPAN><LI class=alt><SPAN>$new_array[] = $arr[$keysort[$ii]]; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>return $new_array; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong><span> </span> </li> </ol>