我们在学习php时候,我们可能为php的多维数组排序的问题感到困惑,文章这里就详细的给大家介绍一下php多维数组排序的原理。php也允许在多维数组上执行一些比较复杂的排序——例如,首先对一个嵌套数组使用一个普通的关键字进行排序,然后再根据另一个关键字进行排序。这与使用sql的order by语句对多个字段进行排序非常相似。为了能更好的明白它是如何工作的,请仔细看所举的例子:
<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>></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></SPAN><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></SPAN><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></SPAN><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></SPAN><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></SPAN><LI class=""><SPAN>$name[$key] = $value['name']; </SPAN><LI class=alt><SPAN>$rating[$key] = $value['rating']; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>array_multisort($rating, $name, $data); print_r($data);</SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><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 class=""><SPAN>( </SPAN><LI class=alt><SPAN>[id] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 2 </SPAN></SPAN><LI class=""><SPAN>[name] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> Take That </SPAN></SPAN><LI class=alt><SPAN>[rating] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 1 </SPAN></SPAN><LI class=""><SPAN>) [1] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> Array </SPAN></SPAN><LI class=alt><SPAN>( </SPAN><LI class=""><SPAN>[id] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 1 </SPAN></SPAN><LI class=alt><SPAN>[name] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> Boney M </SPAN></SPAN><LI class=""><SPAN>[rating] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 3 </SPAN></SPAN><LI class=alt><SPAN>) </SPAN><LI class=""><SPAN>[2] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> Array </SPAN></SPAN><LI class=alt><SPAN>( </SPAN><LI class=""><SPAN>[id] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 4 </SPAN></SPAN><LI class=alt><SPAN>[name] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> Lusain </SPAN></SPAN><LI class=""><SPAN>[rating] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 3 </SPAN></SPAN><LI class=alt><SPAN>) </SPAN><LI class=""><SPAN>[3] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> Array </SPAN></SPAN><LI class=alt><SPAN>( </SPAN><LI class=""><SPAN>[id] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 3 </SPAN></SPAN><LI class=alt><SPAN>[name] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> The Killers </SPAN></SPAN><LI class=""><SPAN>[rating] =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 4 </SPAN></SPAN><LI class=alt><SPAN>) </SPAN><LI class=""><SPAN>) </SPAN></LI></OL>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号