Home >Backend Development >PHP Tutorial >Quickly master PHP multidimensional array sorting method_PHP tutorial

Quickly master PHP multidimensional array sorting method_PHP tutorial

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

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>

array_multisort() function is the most useful in PHP One of the functions, it has a very wide range of applications. In addition, as you can see in the example, it can sort multiple unrelated arrays, it can also use one element as the basis for the next sort, and it can also sort database result sets. These examples should give you a preliminary understanding of the use of PHP's multidimensional array sorting functions, and also show you some of the internal functions hidden in the PHP array processing toolkit. Here, we simulate a row and column array in the $data array. I then use the array_multisort() function to reorder the data set, first by rating, and then, if the ratings are equal, by name. Its output is as follows:
<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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446444.htmlTechArticleWhen we are learning PHP, we may be confused by the problem of multi-dimensional array sorting in PHP. The article is detailed here Let me introduce to you the principle of multi-dimensional array sorting in PHP. PHP also allows...
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