Sorting Array Values in PHP(数组排序)的相关代码详解

来源:爱站网时间:2021-07-01编辑:网友分享
最近有不少人在问PHP可以用assort直接排序吗?因为有时候,是需要对对数组内的值进行排序的,接下来,爱站技术频道小编将为大家分享Sorting Array Values in PHP(数组排序)的相关代码详解,感兴趣的朋友可以参考一下。
复制代码 代码如下:

$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:

 

 


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.

以上就是关于Sorting Array Values in PHP(数组排序)的相关内容,如果还想要了解更多关于PHP编程的知识,可以继续关注爱站技术频道,相信可以找到不少你想要的知识内容。

上一篇:PHP中如何用pathinfo()获取文件路径等相关信息

下一篇:PHP中setcookie指定domain参数后为什么在IE下无法设置cookie

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载