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 相关文章推荐
PHP5.3.1 不再支持ISAPI
Jan 08 PHP
php DOS攻击实现代码(附如何防范)
May 29 PHP
PHP 面向对象程序设计(oop)学习笔记 (二) - 静态变量的属性和方法及延迟绑定
Jun 12 PHP
destoon后台网站设置变成空白的解决方法
Jun 21 PHP
PHP中文乱码解决方案
Mar 05 PHP
Yii2.0 Basic代码中路由链接被转义的处理方法
Sep 21 PHP
PHP实现简单ajax Loading加载功能示例
Dec 28 PHP
php 从指定数字中获取随机组合的简单方法(推荐)
Apr 05 PHP
php文件上传及下载附带显示文件及目录功能
Apr 27 PHP
PHP中关键字interface和implements详解
Jun 14 PHP
Laravel5.7 Eloquent ORM快速入门详解
Apr 12 PHP
php字符串过滤strip_tags()函数用法实例分析
Jun 24 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 之Section与Cookie使用总结
2012/09/14 PHP
php实现使用正则将文本中的网址转换成链接标签
2014/12/03 PHP
JavaScript 页面坐标相关知识整理
2010/01/09 Javascript
js 小数取整的函数
2010/05/10 Javascript
JQUERY的属性选择符和自定义选择符使用方法(二)
2011/04/07 Javascript
jQuery选择器的工作原理和优化分析
2011/07/25 Javascript
JS实现遮罩层效果的简单实例
2013/11/12 Javascript
浅析JavaScript中的同名标识符优先级
2013/12/06 Javascript
jQuery中:selected选择器用法实例
2015/01/04 Javascript
Javascript中prototype属性实现给内置对象添加新的方法
2015/05/14 Javascript
Nodejs初级阶段之express
2015/11/23 NodeJs
AngularJS模块学习之Anchor Scroll
2016/01/19 Javascript
浅谈js基本数据类型和typeof
2016/08/09 Javascript
js style.display=block显示布局错乱问题的解决方法
2016/09/21 Javascript
Express下采用bcryptjs进行密码加密的方法
2018/02/07 Javascript
使用vue-router beforEach实现判断用户登录跳转路由筛选功能
2018/06/25 Javascript
jQuery pagination分页示例详解
2018/10/23 jQuery
MockJs结合json-server模拟后台数据
2020/08/26 Javascript
微信小程序实现传递多个参数与事件处理
2019/08/12 Javascript
python登录豆瓣并发帖的方法
2015/07/08 Python
python 实现在Excel末尾增加新行
2018/05/02 Python
对python的输出和输出格式详解
2018/12/08 Python
在python下使用tensorflow判断是否存在文件夹的实例
2019/06/10 Python
详解PyCharm+QTDesigner+PyUIC使用教程
2019/06/13 Python
django 使用全局搜索功能的实例详解
2019/07/18 Python
Python SELENIUM上传文件或图片实现过程
2019/10/28 Python
tensorflow 模型权重导出实例
2020/01/24 Python
解决flask接口返回的内容中文乱码的问题
2020/04/03 Python
如何使用PyCharm将代码上传到GitHub上(图文详解)
2020/04/27 Python
pycharm sciview的图片另存为操作
2020/06/01 Python
一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃
2020/11/02 Python
伦敦平价潮流珠宝首饰品牌:Astrid & Miyu
2016/10/10 全球购物
升旗仪式主持词
2014/03/19 职场文书
地震捐款倡议书
2014/08/29 职场文书
客户答谢会致辞
2015/07/30 职场文书
Python爬虫 简单介绍一下Xpath及使用
2022/04/26 Python