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中的超全局变量
Oct 09 PHP
linux下 C语言对 php 扩展
Dec 14 PHP
PHP的变量总结 新手推荐
Apr 18 PHP
PHP 中关于ord($str)>0x80的详细说明
Sep 23 PHP
php中serialize序列化与json性能测试的示例分析
Apr 27 PHP
简单说说PHP优化那些事(经验分享)
Nov 27 PHP
php正则替换处理HTML页面的方法
Jun 17 PHP
smarty内部日期函数html_select_date()用法实例分析
Jul 08 PHP
PHP7新特性foreach 修改示例介绍
Aug 26 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
Apr 27 PHP
PHP htmlspecialchars() 函数实例代码及用法大全
Sep 18 PHP
php apache开启跨域模式过程详解
Jul 08 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中的一个中文字符串截取函数
2007/02/14 PHP
php下网站防IP攻击代码,超级实用
2010/10/24 PHP
兼容性最强的PHP生成缩略图的函数代码(修改版)
2011/01/18 PHP
PHP十六进制颜色随机生成器功能示例
2017/07/24 PHP
使用php自动备份数据库表的实现方法
2017/07/28 PHP
Laravel框架在本地虚拟机快速安装的方法详解
2018/06/11 PHP
Laravel 解决composer相关操作提示php相关异常的问题
2019/10/23 PHP
不懂JavaScript应该怎样学
2008/04/16 Javascript
Java/JS获取flash高宽的具体方法
2013/12/27 Javascript
LABjs、RequireJS、SeaJS的区别
2014/03/04 Javascript
jquery实现select选中行、列合计示例
2014/04/25 Javascript
解决微信浏览器Javascript无法使用window.location.reload()刷新页面
2016/06/21 Javascript
在javascript中使用com组件的简单实现方法
2016/08/17 Javascript
浅析Node.js非对称加密方法
2018/01/29 Javascript
解决Vue开发中对话框被遮罩层挡住的问题
2018/11/26 Javascript
JavaScript箭头函数中的this详解
2019/06/19 Javascript
解决node终端下运行js文件不支持ES6语法
2020/04/04 Javascript
python基础之入门必看操作
2017/07/26 Python
python机器学习理论与实战(二)决策树
2018/01/19 Python
python之cv2与图像的载入、显示和保存实例
2018/12/05 Python
Python中的元组介绍
2019/01/28 Python
python中如何使用insert函数
2020/01/09 Python
python实现飞行棋游戏
2020/02/05 Python
在pycharm中创建django项目的示例代码
2020/05/28 Python
一文轻松掌握python语言命名规范规则
2020/06/18 Python
详解Html5微信支付爬坑之路
2018/07/24 HTML / CSS
详解HTML5 data-* 自定义属性
2018/01/24 HTML / CSS
网络教育自我鉴定
2013/11/01 职场文书
工作失误检讨书范文大全
2014/01/13 职场文书
买卖合同协议书范本
2014/10/18 职场文书
人事任命通知书
2015/04/21 职场文书
大学生村官入党自传
2015/06/26 职场文书
三好学生主要事迹材料
2015/11/03 职场文书
2020年基层司法所建设情况调研报告
2019/11/30 职场文书
Spring中的@Transactional的工作原理
2022/06/05 Java/Android
Elasticsearch6.2服务器升配后的bug(避坑指南)
2022/09/23 Servers