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中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
Nov 10 PHP
php FLEA中二叉树数组的遍历输出
Sep 26 PHP
php遍历文件夹下的所有文件和子文件夹示例
Mar 20 PHP
ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法
Aug 08 PHP
9段PHP实用功能的代码推荐
Oct 14 PHP
php curl 上传文件代码实例
Apr 27 PHP
Zend Framework教程之Autoloading用法详解
Mar 08 PHP
Yii2中事务的使用实例代码详解
Sep 07 PHP
php+webSoket实现聊天室示例代码(附源码)
Feb 17 PHP
thinkPHP5.0框架模块设计详解
Mar 18 PHP
Laravel 5.5基于内置的Auth模块实现前后台登陆详解
Dec 21 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
Feb 16 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实现的简易版图片相似度比较
2015/01/07 PHP
PHP实现双链表删除与插入节点的方法示例
2017/11/11 PHP
浅析PHP开发规范
2018/02/05 PHP
windows系统php环境安装swoole具体步骤
2021/03/04 PHP
基于jquery的二级联动菜单实现代码
2011/04/25 Javascript
jQuery手机浏览器中拖拽动作的艰难性分析
2015/02/04 Javascript
jQuery插件formValidator实现表单验证
2016/05/23 Javascript
Angular2 PrimeNG分页模块学习
2017/01/14 Javascript
layer弹出层中H5播放器全屏出错的解决方法
2017/02/21 Javascript
jQuery插件FusionCharts绘制2D柱状图和折线图的组合图效果示例【附demo源码】
2017/04/10 jQuery
详解angularjs获取元素以及angular.element()用法
2017/07/25 Javascript
javascript 面向对象实战思想分享
2017/09/07 Javascript
es6函数之rest参数用法实例分析
2020/04/18 Javascript
[05:34]2014DOTA2国际邀请赛中国区预选赛精彩TOPPLAY第二弹
2014/06/25 DOTA
[03:10]2014DOTA2 TI马来劲旅Titan首战告捷目标只是8强
2014/07/10 DOTA
Python中__init__和__new__的区别详解
2014/07/09 Python
python logging类库使用例子
2014/11/22 Python
Python单例模式实例详解
2017/03/01 Python
Pandas Shift函数的基础入门学习笔记
2018/11/16 Python
Python3 Tkinkter + SQLite实现登录和注册界面
2019/11/19 Python
Python Django中间件使用原理及流程分析
2020/06/13 Python
详解HTML5中的元素与元素
2015/08/17 HTML / CSS
荷兰网上鞋店:Ziengs.nl
2017/01/02 全球购物
SheIn沙特阿拉伯:女装在线
2020/03/23 全球购物
介绍一下内联、左联、右联
2013/12/31 面试题
客户代表实习人员自我鉴定
2013/09/27 职场文书
敬老院院长事迹材料
2014/05/21 职场文书
入股协议书范本
2014/11/01 职场文书
三峡导游词
2015/01/31 职场文书
导师工作推荐信
2015/03/27 职场文书
学校会议通知范文
2015/04/15 职场文书
2015年度绩效考核工作总结
2015/05/27 职场文书
学困生帮扶工作总结
2015/08/13 职场文书
2019年共青团工作条例最新版
2019/11/12 职场文书
Spring Bean是如何初始化的详解
2022/03/22 Java/Android
Python使用Beautiful Soup(BS4)库解析HTML和XML
2022/06/05 Python