首頁 >php教程 >php手册 >快速掌握PHP多维数组排序方法

快速掌握PHP多维数组排序方法

WBOY
WBOY原創
2016-06-13 11:05:17921瀏覽

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

array_multisort()函数是PHP中最有用的函数之一,它有非常广泛的应用范围。另外,就如你在例子中所看到的,它能对多个不相关的数组进行排序,也可以使用其中的一个元素作为下次排序的基础,还可以对数据库结果集进行排序。这些例子应该让你对PHP多维数组排序函数的使用有了初步的了解,也向你展示了一些隐藏在PHP数组处理工具包的内部功能。这里,我们在$data数组中模拟了一个行和列数组。然后,我使用array_multisort()函数对数据集合进行重排,首先是根据rating进行排序,然后,如果rating相等的话,再根据name排序。它的输出结果如下:
<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>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn