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 var_dump遍历对象属性的函数与应用代码
Jun 04 PHP
PHP MySQL应用中使用XOR运算加密算法分享
Aug 28 PHP
php中的常用魔术方法总结
Aug 02 PHP
经典PHP加密解密函数Authcode()修复版代码
Apr 05 PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
Apr 07 PHP
php防止用户重复提交表单
Nov 02 PHP
PHP读取文件内容的五种方式
Dec 28 PHP
WordPress开发中短代码的实现及相关函数使用技巧
Jan 05 PHP
Zend Framework动作助手Redirector用法实例详解
Mar 05 PHP
利用PHP自动生成印有用户信息的名片
Aug 01 PHP
ThinkPHP5.0框架验证码功能实现方法【基于第三方扩展包】
Mar 11 PHP
关于Laravel参数验证的一些疑与惑
Nov 19 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
Amazon Prime Video平台《无限住人 -IMMORTAL-》2020年开始TV放送!
2020/03/06 日漫
PHP CURL获取cookies模拟登录的方法
2013/11/04 PHP
PHP两种去掉数组重复值的方法比较
2014/06/19 PHP
用js实现的一个Flash滚动轮换显示图片代码生成器
2007/03/14 Javascript
JavaScript打字小游戏代码
2011/12/26 Javascript
setTimeout的延时为0时多个浏览器的区别
2012/05/23 Javascript
checkbox设置复选框的只读效果不让用户勾选
2013/08/12 Javascript
js替代copy(示例代码)
2013/11/27 Javascript
JavaScript中toString()方法的使用详解
2015/06/05 Javascript
javascript中JSON对象与JSON字符串相互转换实例
2015/07/11 Javascript
Angularjs中使用Filters详解
2016/03/11 Javascript
javascript replace()第二个参数为函数时的参数用法
2016/12/26 Javascript
JavaScript函数柯里化原理与用法分析
2017/03/31 Javascript
vue实现单选和多选功能
2017/08/11 Javascript
对Angular中单向数据流的深入理解
2018/03/31 Javascript
Vue EventBus自定义组件事件传递
2018/06/25 Javascript
vue2.0父子组件间传递数据的方法
2018/08/16 Javascript
微信小程序实现顶部下拉菜单栏
2018/11/04 Javascript
详解微信小程序文件下载--视频和图片
2019/04/24 Javascript
通过seajs实现JavaScript的模块开发及按模块加载
2019/06/06 Javascript
原生js实现购物车
2020/09/23 Javascript
vue动态设置路由权限的主要思路
2021/01/13 Vue.js
[58:23]LGD vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
python中常用检测字符串相关函数汇总
2015/04/15 Python
Python教程之全局变量用法
2016/06/27 Python
彻彻底底地理解Python中的编码问题
2018/10/15 Python
Python中extend和append的区别讲解
2019/01/24 Python
Python偏函数Partial function使用方法实例详解
2020/06/17 Python
Python爬虫设置ip代理过程解析
2020/07/20 Python
一款纯css3实现的动画加载导航
2014/10/08 HTML / CSS
家乐福台湾线上购物网:Carrefour台湾
2020/09/15 全球购物
人事主管的岗位职责
2013/11/16 职场文书
保安队长职务说明书
2014/02/23 职场文书
村道德模范事迹材料
2014/08/28 职场文书
2016年社区文体活动总结
2016/04/06 职场文书
Django drf请求模块源码解析
2021/06/08 Python