Sorting Array Values in PHP(数组排序)


Posted in PHP onSeptember 15, 2011
$full_name = array(); 
$full_name["Roger"] = "Waters"; 
$full_name["Richard"] = "Wright"; 
$full_name["Nick"] = "Mason"; 
$full_name["David"] = "Gilmour";

To sort this array, you just use the assort( ) function. This involves nothing more complex than typing the word asort, followed by round brackets. In between the round brackets, type in the name of your Associative array:
asort($full_name);

The letter "a" tells PHP that the array is an Associative one. (If you don't have the "a" before "sort", your key names will turn in to numbers!). The "a" also tells PHP to sort by the Value, and NOT by the key. In our script above, the surnames will be sorted. If you want to sort using the Key, then you can use ksort() instead.

If you have a Scalar array (numbers as Keys), then you leave the "a" off. Like this:

$numbers = array( ); 
$numbers[]="2"; 
$numbers[]="8"; 
$numbers[]="10"; 
$numbers[]="6"; 
sort($numbers); 
print $numbers[0] ; 
print $numbers[1]; 
print $numbers[2] ; 
print $numbers[3];

The numbers are then sorted from lowest to highest. If you want to sort in reverse order then you need the following:

rsort( ) ? Sorts a Scalar array in reverse order
arsort( ) - Sorts the Values in an Associative array in reverse order
krsort( ) - Sorts the Keys in an Associative array in reverse order

In the next part, we look at how to get a random value from an array.

PHP 相关文章推荐
如何正确理解PHP的错误信息
Oct 09 PHP
php类
Nov 27 PHP
php防注
Jan 15 PHP
php设计模式 Bridge (桥接模式)
Jun 26 PHP
如何在Ubuntu下启动Apache的Rewrite功能
Jul 05 PHP
模板引擎smarty工作原理以及使用示例
May 25 PHP
PHP进程同步代码实例
Feb 12 PHP
typecho插件编写教程(二):写一个新插件
May 28 PHP
php获取图片信息的方法详解
Dec 10 PHP
详解PHP实现定时任务的五种方法
Jul 25 PHP
PHP编程获取图片的主色调的方法【基于Imagick扩展】
Aug 02 PHP
laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子
Nov 14 PHP
PHP 图片上传代码
Sep 13 #PHP
php中json_encode中文编码问题分析
Sep 13 #PHP
PHP pathinfo()获得文件的路径、名称等信息说明
Sep 13 #PHP
PHP获取MAC地址的函数代码
Sep 11 #PHP
PHP内核介绍及扩展开发指南―基础知识
Sep 11 #PHP
PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍
Sep 11 #PHP
20个PHP常用类库小结
Sep 11 #PHP
You might like
关于PHP结束标签的使用细节探讨及联想
2013/03/04 PHP
php基于jquery的ajax技术传递json数据简单实例
2016/04/15 PHP
Javascript 篱式条件判断
2008/08/22 Javascript
javascript中万恶的function实例分析
2011/05/25 Javascript
Jquery ajax传递复杂参数给WebService的实现代码
2011/08/08 Javascript
理清apply(),call()的区别和关系
2011/08/14 Javascript
S2SH整合JQuery+Ajax实现登录验证功能实现代码
2013/01/30 Javascript
自己动手实现jQuery Callbacks完整功能代码详解
2013/11/25 Javascript
实例分析js和C#中使用正则表达式匹配a标签
2014/11/26 Javascript
jQuery实现“扫码阅读”功能
2015/01/21 Javascript
JS实现方向键切换输入框焦点的方法
2015/08/19 Javascript
BootStrap初学者对弹出框和进度条的使用感觉
2016/06/27 Javascript
详解AngularJS中ng-src指令的使用
2016/09/07 Javascript
利用bootstrapValidator验证UEditor
2016/09/14 Javascript
jQuery中的on与bind绑定事件区别实例详解
2017/02/28 Javascript
2种在vue项目中使用百度地图的简单方法
2018/09/28 Javascript
详解JavaScript 新语法之Class 的私有属性与私有方法
2019/04/23 Javascript
浅谈javascript错误处理
2019/08/11 Javascript
js实现网页版贪吃蛇游戏
2020/02/22 Javascript
JavaScript中如何调用Java方法
2020/09/16 Javascript
Python实现简单的文件传输与MySQL备份的脚本分享
2016/01/03 Python
python连接PostgreSQL数据库的过程详解
2019/09/18 Python
Python使用py2neo操作图数据库neo4j的方法详解
2020/01/13 Python
关于Python错误重试方法总结
2021/01/03 Python
HTML5实现移动端复制功能
2018/04/19 HTML / CSS
Pretty You London官网:英国拖鞋和睡衣品牌
2019/05/08 全球购物
怎样创建、运行java程序
2014/08/01 面试题
软件测试面试题
2015/10/21 面试题
班主任工作年限证明
2014/01/12 职场文书
机械操作工岗位职责
2014/08/08 职场文书
竞聘自述材料
2014/08/25 职场文书
大学生预备党员自我评价
2015/03/04 职场文书
幼儿园教师工作总结2015
2015/04/02 职场文书
大学生暑假实习总结
2015/07/13 职场文书
趣味运动会加油词
2015/07/18 职场文书
K8s部署发布Golang应用程序的实现方法
2021/07/16 Golang