我们在学习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> <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>