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中养成7个面向对象的好习惯
Jan 28 PHP
PHP set_time_limit(0)长连接的实现分析
Mar 02 PHP
深入理解PHP中的Session和Cookie
Jun 21 PHP
浅谈web上存漏洞及原理分析、防范方法(文件名检测漏洞)
Jun 29 PHP
PHP编写daemon process 实例详解
Nov 13 PHP
yii2实现 "上一篇,下一篇" 功能的代码实例
Feb 04 PHP
PHP迭代与递归实现无限级分类
Aug 28 PHP
php实现微信发红包功能
Jul 13 PHP
php微信公众号开发之翻页查询
Oct 20 PHP
PHP PDOStatement::getColumnMeta讲解
Feb 01 PHP
laravel清除视图缓存的代码
Oct 23 PHP
laravel框架数据库操作、查询构建器、Eloquent ORM操作实例分析
Dec 20 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
ThinkPHP关联模型操作实例分析
2012/09/23 PHP
php实现的漂亮分页方法
2014/04/17 PHP
php+xml结合Ajax实现点赞功能完整实例
2015/01/30 PHP
Ubuntu 16.04下安装PHP 7过程详解
2017/03/28 PHP
Jquery选择器 $实现原理
2009/12/02 Javascript
jquery 插件学习(五)
2012/08/06 Javascript
有关于eclipse配置spket需要注意的一些地方
2013/04/07 Javascript
js获取ajax返回值代码
2014/04/30 Javascript
JS实现带有3D立体感的银灰色竖排折叠菜单代码
2015/10/20 Javascript
使用jQuery判断浏览器滚动条位置的方法
2016/05/30 Javascript
angular中的http拦截器Interceptors的实现
2017/02/21 Javascript
jquery submit()不能提交表单的解决方法
2017/04/24 jQuery
BootStrap Table 后台数据绑定、特殊列处理、排序功能
2017/05/27 Javascript
Bootstrap输入框组件使用详解
2017/06/09 Javascript
Node.js  事件循环详解及实例
2017/08/06 Javascript
Vue 自定义指令功能完整实例
2019/09/17 Javascript
Vue 监听元素前后变化值实例
2020/07/29 Javascript
element-ui 弹窗组件封装的步骤
2021/01/22 Javascript
[59:08]Ti4 冒泡赛第二天 NEWBEE vs Titan 2
2014/07/15 DOTA
Python中比较特别的除法运算和幂运算介绍
2015/04/05 Python
python结合opencv实现人脸检测与跟踪
2015/06/08 Python
python写入已存在的excel数据实例
2018/05/03 Python
python3 cvs将数据读取为字典的方法
2018/12/22 Python
解决keras,val_categorical_accuracy:,0.0000e+00问题
2020/07/02 Python
python 如何对logging日志封装
2020/12/02 Python
详解python的变量缓存机制
2021/01/24 Python
css3实现垂直下拉动画菜单示例
2014/04/22 HTML / CSS
Ajax实现页面无刷新留言效果
2021/03/24 Javascript
教师现实表现材料
2014/02/14 职场文书
应届毕业生自荐信例文
2014/02/26 职场文书
校车安全责任书
2014/08/25 职场文书
物业工程部经理岗位职责
2015/04/09 职场文书
医院病假条怎么写
2015/08/17 职场文书
大学班干部竞选稿
2015/11/20 职场文书
详解TS数字分隔符和更严格的类属性检查
2021/05/06 Javascript
js之ajax文件上传
2021/05/13 Javascript