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静态类
Nov 25 PHP
php mysql索引问题
Jun 07 PHP
PHP 根据IP地址控制访问的代码
Apr 22 PHP
关于php连接mssql:pdo odbc sql server
Jul 20 PHP
php 记录进行累加并显示总时长为秒的结果
Nov 04 PHP
PHP中HTML标签过滤技巧
Jan 07 PHP
PHP面向对象教程之自定义类
Jun 10 PHP
PHP自带函数给数字或字符串自动补齐位数
Jul 29 PHP
PHP使用递归生成文章树
Apr 21 PHP
php 数组元素快速去重
May 05 PHP
PHP实现微信退款功能
Oct 02 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
Feb 27 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实现不通过扩展名准确判断文件类型的方法【finfo_file方法与二进制流】
2017/04/18 PHP
php 判断IP为有效IP地址的方法
2018/01/28 PHP
JavaScript 动态将数字金额转化为中文大写金额
2009/05/14 Javascript
js 金额文本框实现代码
2012/02/14 Javascript
Node.js配合node-http-proxy解决本地开发ajax跨域问题
2016/08/31 Javascript
jQuery基本筛选选择器实例代码
2017/02/06 Javascript
tablesorter.js表格排序使用方法(支持中文排序)
2017/02/10 Javascript
JavaScript数据结构之数组的表示方法示例
2017/04/12 Javascript
原生JS实现Ajax跨域请求flask响应内容
2017/10/24 Javascript
Vue监听事件实现计数点击依次增加的方法
2018/09/26 Javascript
Iview Table组件中各种组件扩展的使用
2018/10/20 Javascript
基于Proxy的小程序状态管理实现
2019/06/14 Javascript
微信小程序实现注册登录功能(表单校验、错误提示)
2019/12/10 Javascript
node爬取新型冠状病毒的疫情实时动态
2020/02/06 Javascript
在Python中利用Pandas库处理大数据的简单介绍
2015/04/07 Python
在Docker上部署Python的Flask框架的教程
2015/04/08 Python
python写入并获取剪切板内容的实例
2018/05/31 Python
python 平衡二叉树实现代码示例
2018/07/07 Python
python打印n位数“水仙花数”(实例代码)
2019/12/25 Python
踩坑:pytorch中eval模式下结果远差于train模式介绍
2020/06/23 Python
解决pytorch 交叉熵损失输出为负数的问题
2020/07/07 Python
Python爬虫之Selenium库的使用方法
2021/01/03 Python
Otticanet美国:最顶尖的世界名牌眼镜, 能得到打折季的价格
2019/03/10 全球购物
初中校园之声广播稿
2014/01/15 职场文书
自荐信的格式
2014/03/10 职场文书
向国旗敬礼活动总结范文2014
2014/09/27 职场文书
公安局副政委班子个人对照检查材料
2014/10/04 职场文书
工作作风整顿个人剖析材料
2014/10/11 职场文书
2014年作风建设工作总结
2014/10/29 职场文书
2014年初中班主任工作总结
2014/11/08 职场文书
2015年党员自我剖析材料
2014/12/17 职场文书
中学教师师德师风承诺书
2015/04/28 职场文书
Python基础知识之变量的详解
2021/04/14 Python
Pycharm 如何设置HTML文件自动补全代码或标签
2021/05/21 Python
vue中利用mqtt服务端实现即时通讯的步骤记录
2021/07/01 Vue.js
MySQL库表太大怎么办? 数据库分库分表项目实践
2022/04/11 MySQL