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 学习路线与时间表
Feb 21 PHP
PHP调用Twitter的RSS的实现代码
Mar 10 PHP
PHP的MVC模式实现原理分析(一相简单的MVC框架范例)
Apr 29 PHP
从零开始学YII2框架(五)快速生成代码工具 Gii 的使用
Aug 20 PHP
PHP中trim()函数简单使用指南
Apr 16 PHP
大家在抢红包,程序员在研究红包算法
Aug 31 PHP
修复ShopNC使用QQ 互联时提示100010 错误
Nov 08 PHP
php 指定范围内多个随机数代码实例
Jul 18 PHP
php中让人头疼的浮点数运算分析
Oct 10 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 PHP
Docker搭建自己的PHP开发环境
Feb 24 PHP
laravel 解决crontab不执行的问题
Oct 22 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判断远程图片是否存在的几种方法
2014/05/04 PHP
php使用curl实现简单模拟提交表单功能
2017/05/15 PHP
php微信公众号开发之音乐信息
2018/10/20 PHP
YUI的Tab切换实现代码
2010/04/11 Javascript
基于jquery的$.ajax async使用
2011/10/19 Javascript
js生成随机数的过程解析
2015/11/24 Javascript
JS中取二维数组中最大值的方法汇总
2016/04/17 Javascript
jQuery判断元素是否显示 是否隐藏的简单实现代码
2016/05/19 Javascript
js删除数组元素、清空数组的简单方法(必看)
2016/07/27 Javascript
基于BootStrap实现局部刷新分页实例代码
2016/08/08 Javascript
浅谈js中的三种继承方式及其优缺点
2016/08/10 Javascript
React组件之间的通信的实例代码
2017/06/27 Javascript
Vue.js点击切换按钮改变内容的实例讲解
2018/08/22 Javascript
详解Nuxt.js中使用Element-UI填坑
2019/09/06 Javascript
js实现无缝轮播图特效
2020/05/09 Javascript
[01:25]DOTA2自定义游戏灵园鬼域等你踏足
2015/10/30 DOTA
[05:03]2018DOTA2亚洲邀请赛主赛事首日回顾
2018/04/04 DOTA
Python实现获取某天是某个月中的第几周
2015/02/11 Python
python绘制热力图heatmap
2020/03/23 Python
Form表单及django的form表单的补充
2019/07/25 Python
Python使用itchat模块实现群聊转发,自动回复功能示例
2019/08/26 Python
Python调用Windows命令打印文件
2020/02/07 Python
Python json转字典字符方法实例解析
2020/04/13 Python
详解Python3.8+PyQt5+pyqt5-tools+Pycharm配置详细教程
2020/11/02 Python
Python类型转换的魔术方法详解
2020/12/23 Python
python regex库实例用法总结
2021/01/03 Python
10张动图学会python循环与递归问题
2021/02/06 Python
英国在线电子和小工具商店:TecoBuy
2018/10/06 全球购物
Interflora澳大利亚:同日鲜花速递
2019/06/25 全球购物
英国自行车商店:AW Cycles
2021/02/24 全球购物
大学毕业生文采飞扬的自我鉴定
2013/12/03 职场文书
模具专业毕业推荐信
2014/03/08 职场文书
毕业大学生自荐信
2014/06/17 职场文书
农业局党的群众路线教育实践活动整改方案
2014/09/20 职场文书
2015年实习班主任工作总结
2015/04/23 职场文书
MySQL表类型 存储引擎 的选择
2021/11/11 MySQL