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 a simple smtp class
Nov 26 PHP
php whois查询API制作方法
Jun 23 PHP
PHP5权威编程阅读学习笔记 附电子书下载
Jul 05 PHP
php eval函数用法 PHP中eval()函数小技巧
Oct 31 PHP
浅析linux下apache服务器的配置和管理
Aug 10 PHP
php MessagePack介绍
Oct 06 PHP
php中使用getimagesize获取图片、flash等文件的尺寸信息实例
Apr 29 PHP
PHP的switch判断语句的“高级”用法详解
Oct 01 PHP
php序列化函数serialize() 和 unserialize() 与原生函数对比
May 08 PHP
Yii2使用$this->context获取当前的Module、Controller(控制器)、Action等
Mar 29 PHP
Laravel学习教程之request validation的编写
Oct 25 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
Dec 25 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 分页分组类
2009/12/10 PHP
PHP的Yii框架的常用日志操作总结
2015/12/08 PHP
PHP打印输出函数汇总
2016/08/28 PHP
js 省地市级联选择
2010/02/07 Javascript
如何确保JavaScript的执行顺序 之实战篇
2011/03/03 Javascript
基于jQuery的动态表格插件
2011/03/28 Javascript
jquery实现简单易懂的图片展示小例子
2013/11/21 Javascript
javascript学习笔记(七)Ajax和Http状态码
2014/10/08 Javascript
FF(火狐)浏览器无法执行window.close()解决方案
2014/11/13 Javascript
Jquery实现仿腾讯微博发表广播
2014/11/17 Javascript
javascript实现的右下角弹窗实例
2015/04/24 Javascript
全面解析JavaScript的Backbone.js框架中的Router路由
2016/05/05 Javascript
nodejs基础知识
2017/02/03 NodeJs
H5上传本地图片并预览功能
2017/05/08 Javascript
一篇文章让你彻底弄懂JS的事件冒泡和事件捕获
2017/08/14 Javascript
vue 实现剪裁图片并上传服务器功能
2018/03/01 Javascript
nuxt框架中路由鉴权之Koa和Session的用法
2018/05/09 Javascript
jquery引入外部CDN 加载失败则引入本地jq库
2018/05/23 jQuery
JS实现的字符串数组去重功能小结
2019/06/17 Javascript
Python日志模块logging简介
2015/04/13 Python
20招让你的Python飞起来!
2016/09/27 Python
Python 快速实现CLI 应用程序的脚手架
2017/12/05 Python
python进行文件对比的方法
2018/12/24 Python
pyqt5 实现在别的窗口弹出进度条
2019/06/18 Python
Python解释器及PyCharm工具安装过程
2020/02/26 Python
苏格兰销售女装、男装和童装的连锁店:M&Co
2018/03/16 全球购物
意大利买卖二手奢侈品网站:LAMPOO
2020/06/03 全球购物
2014年大班元旦活动方案
2014/02/26 职场文书
本科毕业自我鉴定
2014/03/20 职场文书
师德师风演讲稿
2014/05/05 职场文书
2014年大学班级工作总结
2014/11/14 职场文书
承诺书范本大全
2015/05/04 职场文书
婚庆开业庆典主持词
2015/06/30 职场文书
三八红旗手主要事迹材料
2015/11/04 职场文书
经典格言警句:没有热忱,世间便无进步
2019/11/13 职场文书
Oracle更换为MySQL遇到的问题及解决
2021/05/21 Oracle