3

PHP 关于数组排序的函数

 2 years ago
source link: http://froyot.github.io/php/2019/03/06/php_array_sort.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP 关于数组排序的函数

06 Mar 2019 Category: php

php的数组排序函数有很多。有按键排序,有按值排序。有升序,有降序。有的排序后改变原数组索引,有的不改变。

关于PHP的排序函数,官方文档给出了下面的一个总结表:

php array sort functions

以上函数排序结果都是通过引用传递到原数组中去,而不是返回一个新的有序的数组。

一维数组排序

其实PHP内部对于数组排序的实现都比较相似,都是一个模子刻出来的。

先看看asort,arsort排序源码:

php array asort

php array arsort

再来看看sort,rsort排序函数的源码

php array sort

php array rsort

从上面四个函数的代码对比可以看出,数组排序最终都是通过zend_hash_sort实现的。查看源码,可以发现,除了array_multisort是使用zend_sort实现的外,其他的函数都是通过zend_hash_sort实现。排序方式通过传入的排序函数决定,并通过参数控制是否覆盖原来的索引。

按照这个理解,估计有的人会猜想对于用户自定义函数排序,内部是直接把函数传递到zend_has_sort中去。但是在PHP中其实还加了一层,限定了函数只能作用在键或者值之上。对于函数usortuksort分别是使用自定义函数按值,和按键排序。

usort and uksort

用户自定义函数其实是在php_array_user_key_compare,和php_array_user_compare中调用的。

其实归结起来,排序函数就有下面几种

1、sort,按值排序,改变键名,相关有rsort,usort

2、asort,按值排序,不改变键名,相关有arsort,uasort

3、ksort,按键名排序,不改变键名,相关有krsort,uksort

4、nasort,nacasesort自然顺序排序,不改变键名

多数组排序

比如:

```php

array_multisort(
	$volume, SORT_DESC, 
	$edition, SORT_ASC, $data
);


array_multisort(
	$ar[0], SORT_ASC, SORT_STRING,
    $ar[1], SORT_NUMERIC, SORT_DESC
);

还有这样:

array_multisort($ar1, $ar2);

它内部怎么确定传的参数代表什么意思呢?

php array_multisort

可以看到,代码里对数据类型进行判断。如果是数组,都当做排序数组。所以array_multisort可以排序不定个数个数组。顺序,以及排序方式都是通过获取数组之后的整形参数得到。如果没有,那就都默认。

来发评论吧~
Powered By Valine
v1.4.18

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK