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 或网站服务器日志,并正确配置 PHP 安装最快的解决办法
Aug 01 PHP
IP攻击升级,程序改进以对付新的攻击
Nov 23 PHP
PHP数组无限分级数据的层级化处理代码
Dec 29 PHP
解析如何修改phpmyadmin中的默认登陆超时时间
Jun 25 PHP
神盾加密解密教程(一)PHP变量可用字符
May 28 PHP
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
Jun 23 PHP
php继承中方法重载(覆盖)的应用场合
Feb 09 PHP
php实现的任意进制互转类分享
Jul 07 PHP
PHP面试题之文件目录操作
Oct 15 PHP
thinkphp多表查询两表有重复相同字段的完美解决方法
Sep 22 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
Feb 11 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中的ini配置原理详解
2014/10/14 PHP
老生常谈PHP面向对象之解释器模式
2017/05/17 PHP
PHP Socket网络操作类定义与用法示例
2017/08/30 PHP
说说掌握JavaScript语言的思想前提想学习js的朋友可以看看
2009/04/01 Javascript
js计算两个时间之间天数差的实例代码
2013/11/19 Javascript
jQuery中 prop() attr()使用详解
2015/05/19 Javascript
jQuery实现简单的点赞效果
2020/05/29 Javascript
javascript高级选择器querySelector和querySelectorAll全面解析
2016/04/07 Javascript
jquery显示隐藏元素的实现代码
2016/05/19 Javascript
详解js的事件处理函数和动态创建html标记方法
2016/12/16 Javascript
jQuery插件FusionWidgets实现的Cylinder图效果示例【附demo源码】
2017/03/23 jQuery
js实现多行文本框统计剩余字数功能
2017/03/28 Javascript
详解微信小程序 通过控制CSS实现view隐藏与显示
2017/05/24 Javascript
Express+Nodejs 下的登录拦截实现代码
2017/07/01 NodeJs
vue项目中极验验证的使用代码示例
2019/12/03 Javascript
[17:00]DOTA2 HEROS教学视频教你分分钟做大人-帕克
2014/06/10 DOTA
[01:41]DOTA2 2015国际邀请赛中国区预选赛第三日战报
2015/05/28 DOTA
[01:11]辉夜杯战队访谈宣传片—CDEC.Y
2015/12/26 DOTA
[01:03:50]DOTA2-DPC中国联赛 正赛 CDEC vs DLG BO3 第二场 2月7日
2021/03/11 DOTA
python判断图片宽度和高度后删除图片的方法
2015/05/22 Python
Python中几个比较常见的名词解释
2015/07/04 Python
Pythont特殊语法filter,map,reduce,apply使用方法
2016/02/27 Python
Python抓取手机号归属地信息示例代码
2016/11/28 Python
使用PyTorch实现MNIST手写体识别代码
2020/01/18 Python
用python制作个音乐下载器
2021/01/30 Python
matplotlib绘制正余弦曲线图的实现
2021/02/22 Python
使用CSS3配合IE滤镜实现渐变和投影的效果
2015/09/06 HTML / CSS
用HTML5.0制作网页的教程
2010/05/30 HTML / CSS
李维斯牛仔裤荷兰官方网站:Levi’s NL
2020/08/23 全球购物
安全生产计划书
2014/05/04 职场文书
文艺演出策划方案
2014/06/07 职场文书
2014年餐厅服务员工作总结
2014/11/18 职场文书
2015年企业团支部工作总结
2015/05/21 职场文书
《我是什么》教学反思
2016/02/16 职场文书
如何使用分区处理MySQL的亿级数据优化
2021/06/18 MySQL
字节飞书面试promise.all实现示例
2022/06/16 Javascript