Home > Article > Backend Development > In-depth understanding of PHP multidimensional array sorting principle_PHP tutorial
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.
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>
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>