Home >php教程 >php手册 >PHP数组排序函数合集 以及它们之间的联系分析

PHP数组排序函数合集 以及它们之间的联系分析

WBOY
WBOYOriginal
2016-06-06 20:29:171439browse

本篇文章是对PHP数组排序函数合集以及它们之间的联系进行了详细的分析介绍,需要的朋友参考下

下边提到的几个数组函数的排序有一些共性:
1 数组被作为排序函数的参数,排序以后,数组本身就发生了改变,函数的返回值为bool类型。
2 函数名中出现单a表示association,含义为,在按值排序的过程中,保持key=>value的对应关系不变
3 函数名中出现单k表示key,虚拟主机,含义为,在按值排序的过程中按照数组key而不是数组的值排序
4 函数名中出现单r的表示reverse,含义为,按照跟不加r的相反的顺序排列
5 函数名中出现单u的表示user-defined,含义为,使用用户自定义函数排序,如果函数的逻辑是参数1--------------------sort函数升序排序--------------------------------

复制代码 代码如下:


bool sort ( array &$array [, int $sort_flags= SORT_REGULAR ] )
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
var_dump($fruits);
?>
结果:
array
0 =>
string
'apple' (length=5)
1 =>
string
'banana' (length=6)
2 =>
string
'lemon' (length=5)
3 =>
string
'orange' (length=6)


--------------------rsort降序排列--------------------

复制代码 代码如下:


$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
var_dump($fruits);
?>
结果:
array
0 =>
string
'orange' (length=6)
1 =>
string
'lemon' (length=5)
2 =>
string
'banana' (length=6)
3 =>
string
'apple' (length=5)


---------------asort按照二维数组值的升序排列(保持key=>value的关联关系)-----------

复制代码 代码如下:


$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
var_dump($fruits);
?>
结果:
array
'c' =>
string
'apple' (length=5)
'b' =>
string
'banana' (length=6)
'd' =>
string
'lemon' (length=5)
'a' =>
string
'orange' (length=6)


---------arsort按照二维数组值的降序排列(保持key=>value的关联关系)---------

复制代码 代码如下:


$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
arsort($fruits);
var_dump($fruits);
?>
结果
array
'a' =>
string
'orange' (length=6)
'd' =>
string
'lemon' (length=5)
'b' =>
string
'banana' (length=6)
'c' =>
string
'apple' (length=5)


--------------------ksort按照数组的key升序排列--------------

复制代码 代码如下:


$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
var_dump($fruits);
?>
结果
array
'a' =>
string
'orange' (length=6)
'b' =>
string
'banana' (length=6)
'c' =>
string
'apple' (length=5)
'd' =>
string
'lemon' (length=5)


---------------------krsort按照数组key的降序排列-----------------------

复制代码 代码如下:


$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
var_dump($fruits);
?>
array
'd' =>
string
'lemon' (length=5)
'c' =>
string
'apple' (length=5)
'b' =>
string
'banana' (length=6)
'a' =>
string
'orange' (length=6)


----------------usort函数按照用户自定义的函数排序----------------

复制代码 代码如下:


function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a }
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
var_dump($a);
?>


结果:
array
0 =>
int
1
1 =>
int
2
2 =>
int
3
3 =>
int
5
4 =>
int
6
-----------------uksort使用自定义函数按照数组的key排序-----------------

复制代码 代码如下:


function cmp($a, $b)
{
$a = preg_replace('@^(a|an|the) @', '', $a);
$b = preg_replace('@^(a|an|the) @', '', $b);
return strcasecmp($a, $b);
}
$a = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
uksort($a, "cmp");
var_dump($a);
?>


结果:
array
'an apple' =>
int
3
'a banana' =>
int
4
'the Earth' =>
int
2
'John' =>
int
1
--------------uasort将数组用自定义函数按照value排序,网站空间,保持索引关系不变---------

复制代码 代码如下:


// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a }
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
var_dump($array);
// Sort and print the resulting array
uasort($array, 'cmp');
var_dump($array);
?>


结果:
array
'a' =>
int
4
'b' =>
int
8
'c' =>
int
-1
'd' =>
int
-9
'e' =>
int
2
'f' =>
int
5
'g' =>
int
3
'h' =>
int
-4
array
'd' =>
int
-9
'h' =>
int
-4
'c' =>
int
-1
'e' =>
int
2
'g' =>
int
3
'a' =>
int
4
'f' =>
int
5
'b' =>
int
8
-------------------array_multisort排序多个数组或多维数组---------

复制代码 代码如下:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn