PHP函数array_multisort()实现多维数组排序的示例讲解_PHP教程

WBOY
发布: 2016-07-15 13:29:56
原创
833 人浏览过

对于Listing J示例为我们具体说明了PHP函数array_multisort()的工作方式:

<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></li>
<li class="alt"><span><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>array_multisort($rating, $name, $data); print_r($data);</span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
登录后复制

这里,我们在$data数组中模拟了一个行和列数组。然后,我使用PHP函数array_multisort()对数据集合进行重排,首先是根据rating进行排序,然后,如果rating相等的话,再根据name排序。它的输出结果如下:

Array ([0] => Array

(

[id] => 2

[name] => Take That

[rating] => 1

) [1] => Array

(

[id] => 1

[name] => Boney M

[rating] => 3

)

[2] => Array

(

[id] => 4

[name] => Lusain

[rating] => 3

)

[3] => Array

(

[id] => 3

[name] => The Killers

[rating] => 4

)

)

PHP函数array_multisort()是PHP中最有用的函数之一,它有非常广泛的应用范围。另外,就如你在例子中所看到的,它能对多个不相关的数组进行排序,也可以使用其中的一个元素作为下次排序的基础,还可以对数据库结果集进行排序。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446344.htmlTechArticle对于 Listing J示例为我们具体说明了PHP函数array_multisort()的工作方式: ? php $ data = array (array("id"= 1,"name"= "BoneyM","rating"= 3), array("id"= 2,"name"=...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板