Home >Backend Development >PHP Tutorial >Quickly master PHP multidimensional array sorting method_PHP tutorial
When we are learning PHP, we may be confused by the problem of PHP's multi-dimensional array sorting. Here in this article, we will introduce to you in detail the principle of PHP's multi-dimensional array sorting. PHP also allows you to perform some more complex sorting on multidimensional arrays - for example, sorting a nested array first using a common key and then sorting based on another key. This is very similar to using SQL's ORDER BY statement to sort multiple fields. In order to better understand how it works, please look at the example given carefully:
<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 class=attribute><FONT color=#ff0000>data</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(array("id" =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span> 1, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Boney M", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3), </span> </li> <li class=""> <span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Take That", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1), </span> </li> <li class="alt"> <span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "The Killers", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4), </span> </li> <li class=""> <span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Lusain", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3), </span> </li> <li class="alt"> <span>); foreach ($data as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value) { </span> </li> <li class=""><span>$name[$key] = $value['name']; </span></li> <li class="alt"><span>$rating[$key] = $value['rating']; </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <span>array_multisort($rating, $name, $data); print_r($data);</span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
<ol class="dp-xml"> <li class="alt"><span><span>Array ([0] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span></span></li> <li class=""><span>( </span></li> <li class="alt"> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2 </span> </li> <li class=""> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Take That </span> </li> <li class="alt"> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1 </span> </li> <li class=""> <span>) [1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li class=""> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1 </span> </li> <li class="alt"> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Boney M </span> </li> <li class=""> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3 </span> </li> <li class="alt"><span>) </span></li> <li class=""> <span>[2] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li class=""> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4 </span> </li> <li class="alt"> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Lusain </span> </li> <li class=""> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3 </span> </li> <li class="alt"><span>) </span></li> <li class=""> <span>[3] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li class=""> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3 </span> </li> <li class="alt"> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> The Killers </span> </li> <li class=""> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4 </span> </li> <li class="alt"><span>) </span></li> <li class=""><span>) </span></li> </ol>