PHP中数组排序函数详解
1 数组被作为排序函数的参数,排序以后,数组本身就发生了改变,函数的返回值为bool类型.
2 函数名中出现单a表示association,含义为,在按值排序的过程中,保持key=>value的对应关系不变
3 函数名中出现单k表示key,含义为,在按值排序的过程中按照数组key而不是数组的值排序
4 函数名中出现单r的表示reverse,含义为,按照跟不加r的相反的顺序排列
5 函数名中出现单u的表示user-defined,含义为,使用用户自定义函数排序,如果函数的逻辑是参数1<参数2返回负数,则按照升序排列(p1小2返负升)。
sort函数升序排序,代码如下:
- bool sort ( array &$array [, int $sort_flags= SORT_REGULAR ] )
- <?php
- $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降序排列,代码如下:
- <?php
- $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的关联关系,代码如下:
- <?php
- $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的关联关系,代码如下:
- <?php
- $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升序排列,代码如下:
- <?php
- $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的降序排列,代码如下:
- <?php
- $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函数按照用户自定义的函数排序,代码如下:
- <?php
- function cmp($a, $b)
- {
- if ($a == $b) {
- return 0;
- }
- return ($a < $b) ? -1 : 1;
- }
-
- $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排序,代码如下:
- <?php
- 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排序,保持索引关系不变,代码如下:
- <?php
-
- function cmp($a, $b) {
- if ($a == $b) {
- return 0;
- }
- return ($a < $b) ? -1 : 1;
- }
-
-
- $array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
- var_dump($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排序多个数组或多维数组,代码如下:
- <?php
- $ar = array(
- array("10", 11, 100, 100, "a"),
- array( 1, 2, "2", 3, 1)
- );
-
- array_multisort($ar[0], SORT_ASC, SORT_STRING,
- $ar[1], SORT_NUMERIC, SORT_DESC);
- var_dump($ar);
- ?>
-
- array
- 0 =>
- array
- 0 => string '10' (length=2)
- 1 => int 100
- 2 => int 100
- 3 => int 11
- 4 => string 'a' (length=1)
- 1 =>
- array
- 0 => int 1
-
-
- 1 => int 3
- 2 => string '2' (length=1)
- 3 => int 2
- 4 => int 1
说明:
1 上例中:$ar数组优先按照$ar[0]的字符串值升序排列,如果字符串值相等,再按照$ar[1]数组的数字值降序排列.
2 array_multisort函数的任意一个位置的参数如果是数组,表示排序时用的值,如果有多个数组参数,优先按照前边的数组值进行排序,如果是常量,例如
SORT_ASC,SORT_DESC,SORT_REGULAR,SORT_NUMERIC,SORT_STRING.
表示排序方法,数组取值前优先.