Home  >  Article  >  Backend Development  >  About PHP two-dimensional array sorting using key_PHP tutorial

About PHP two-dimensional array sorting using key_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:28:04766browse

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.

It can execute dynamic web pages faster than CGI or Perl. Compared with other programming languages, dynamic pages made with PHP embed the program into the HTML document for execution, and the execution efficiency is much higher than CGI that completely generates HTML tags; compared with the scripting language JavaScript, which is also embedded in the HTML document In contrast, PHP is executed on the server side, making full use of the server's performance; the PHP execution engine will also store PHP programs that users frequently access in memory, so other users do not need to recompile the program when they access the program again. Just execute the code in the memory directly, which is also one of the manifestations of PHP's high efficiency. PHP has very powerful functions. All CGI or JavaScript functions can be implemented by PHP, and it supports almost all popular databases and operating systems.

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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446494.htmlTechArticlePHP 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 an English hypertext preprocessing language (PHP:Hy...
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