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

php中文网
发布: 2016-07-15 13:28:40
原创
990人浏览过

序列猴子开放平台
序列猴子开放平台

具有长序列、多模态、单模型、大数据等特点的超大规模语言模型

序列猴子开放平台 0
查看详情 序列猴子开放平台

我们在学习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>
登录后复制

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 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>
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446444.htmlTechArticle我们在学习PHP时候,我们可能为PHP的多维数组排序的问题感到困惑,文章这里就详细的给大家介绍一下PHP多维数组排序的原理。PHP也允许在...
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号