Sorting Array Values in PHP(数组排序)

2015-01-24信息快讯网
 
$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( ) C 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.
基于ubuntu下nginx+php+mysql安装配置的具体操作步骤
php中serialize序列化与json性能测试的示例分析
input file获得文件根目录简单实现
PHP中操作ini配置文件的方法
基于PHP Web开发MVC框架的Smarty使用说明
基于initPHP的框架介绍
关于PHP的相似度计算函数:levenshtein的使用介绍
PHP字符过滤函数去除字符串最后一个逗号(rtrim)
PHP 自定义错误处理函数trigger_error()
smarty 缓存控制前的页面静态化原理
PHP Directory 函数的详解
php和js如何通过json互相传递数据相关问题探讨
在smarty中调用php内置函数的方法
PHP应用JSON技巧讲解
PHP5.4中json_encode中文转码的变化小结
PHP中header和session_start前不能有输出原因分析
整理的一些实用WordPress后台MySQL操作命令
将博客园(cnblogs.com)数据导入到wordpress的代码
zend framework配置操作数据库实例分析
PHP gbk环境下json_dencode传送来的汉字
利用Ffmpeg获得flv视频缩略图和视频时间的代码
PHP 图片上传代码
php中json_encode中文编码问题分析
PHP pathinfo()获得文件的路径、名称等信息说明
PHP setcookie指定domain参数后,在IE下设置cookie失效的解决方法
PHP MySQL应用中使用XOR运算加密算法分享
phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法
php中使用Curl、socket、file_get_contents三种方法POST提交数据
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
PHP中获取内网用户MAC地址(WINDOWS/linux)的实现代码
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
ThinkPHP 防止表单重复提交的方法
php Smarty初体验二 获取配置信息
php中Smarty模板初体验
php模拟socket一次连接,多次发送数据的实现代码
PHP FOR MYSQL 代码生成助手(根据Mysql里的字段自动生成类文件的)
php中用foreach来操作数组的代码
php入门学习知识点八 PHP中for循环基本应用之九九乘法口绝表
PHP 数据结构 算法描述 冒泡排序 bubble sort
PHP array_multisort()函数的使用札记
©2014-2024 dbsqp.com