Sorting Array Values in PHP(数组排序)
作者:bea
代码如下: $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 not
代码如下:
$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利用iframe实现无刷新文件上传功能的代码
- php json_encode奇怪问题说明
- PHP安全配置详细说明
- 使用PHP遍历文件夹与子目录的函数代码
- 新浪微博API开发简介之用户授权(PHP基础篇)
- PHP+MYSQL会员系统的登陆即权限判断实现代码
- PHP将DateTime对象转化为友好时间显示的实现代码
- php方法调用模式与函数调用模式简例
- php若干单维数组遍历方法的比较
- PHP学习笔记 用户注册模块用户类以及验证码类
- PHP无刷新上传文件实现代码
- PHP下利用shell后台运行PHP脚本,并获取该脚本的Process ID的代码
- php去除重复字的实现代码
- PHP与SQL注入攻击防范小技巧
- php中判断字符串是否全是中文或含有中文的实现代码
- php中模拟POST传递数据的两种方法分享
- php中获得视频时间总长度的另一种方法
- 利用Ffmpeg获得flv视频缩略图和视频时间的代码