In-depth understanding of PHP multidimensional array sorting principle_PHP tutorial

WBOY
Release: 2016-07-15 13:27:11
Original
751 people have browsed it

With the development of the times, more and more people like to use PHP language. Here we will review the function of PHP multi-dimensional array sorting, and hope that everyone can gain something from it. Returns TRUE on success, FALSE on failure. array_multisort() can be used to sort multiple arrays at once, or to sort PHP multidimensional arrays according to one or more dimensions.

The associated (string) key name remains unchanged, but the numeric key name will be re-indexed. The input array is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on. The parameter structure of this function is somewhat unusual, but very flexible. The first parameter must be an array. Each of the following arguments can be an array or a sort flag listed below.

Sort order flags:
◆SORT_ASC - Sort items in ascending order
◆SORT_DESC - Sort items in descending order

Sort type flags:
◆SORT_REGULAR - Sort items in normal order Method comparison
◆SORT_NUMERIC - Compare items based on numeric values ​​
◆SORT_STRING - Compare items based on strings

Two similar sorting flags cannot be specified after each array. The sort flags specified after each array are valid only for that array - before that, the default values ​​SORT_ASC and SORT_REGULAR.

Example 1. Sorting PHP multi-dimensional array

<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>ar1</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>("10", 100, 100, "a");  </SPAN></SPAN><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>(1, 3, "2", 1);  </SPAN></SPAN><LI class=""><SPAN>array_multisort($ar1, $ar2);  </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>var_dump($ar1);  </SPAN><LI class=alt><SPAN>var_dump($ar2);  </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

In this example, after sorting, the first array will contain "10", "a", 100,100. The second array will contain 1,1,"2",3. The order of the items in the second array is exactly the same as the order of the corresponding items (100 and 100) in the first array.

<ol class="dp-xml">
<li class="alt"><span><span>array(4) {  </span></span></li>
<li class="">
<span>[0]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> string(2) "10"  </span>
</li>
<li class="alt">
<span>[1]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> string(1) "a"  </span>
</li>
<li class="">
<span>[2]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> int(100)  </span>
</li>
<li class="alt">
<span>[3]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> int(100)  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>array(4) {  </span></li>
<li class="">
<span>[0]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> int(1)  </span>
</li>
<li class="alt">
<span>[1]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> int(1)  </span>
</li>
<li class="">
<span>[2]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> string(1) "2"  </span>
</li>
<li class="alt">
<span>[3]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> int(3)  </span>
</li>
<li class=""><span>} </span></li>
</ol>
Copy after login



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446531.htmlTechArticleWith the development of the times, more and more people like to use PHP language. Here we will review the PHP multidimensional I hope everyone can benefit from the array sorting function. Returns TRUE if successful,...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template