盐城弘智学历培训
您现在的位置: 盐城弘智教育 > PHP > PHP资料下载 >

PHP数组排序函数合集_PHP教程

时间:2012-10-31 来源:弘智教育 点击:

下边提到的几个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 ] )

01.<?php
02.$fruits = array("lemon", "orange", "banana", "apple");
03.sort($fruits);
04.var_dump($fruits);
05.?>
06.结果:
07.array
08.0 => string 'apple' (length=5)
09.1 => string 'banana' (length=6)
10.2 => string 'lemon' (length=5)
11.3 => string 'orange' (length=6)

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

01.<?php
02.$fruits = array("lemon", "orange", "banana", "apple");
03.rsort($fruits);
04.var_dump($fruits);
05.?>
06.结果:
07.array
08.0 => string 'orange' (length=6)
09.1 => string 'lemon' (length=5)
10.2 => string 'banana' (length=6)
11.3 => string 'apple' (length=5)

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

01.<?php
02.$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
03.asort($fruits);
04.var_dump($fruits);
05.?>
06.结果:
07.array
08.'c' => string 'apple' (length=5)
09.'b' => string 'banana' (length=6)
10.'d' => string 'lemon' (length=5)
11.'a' => string 'orange' (length=6)

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

01.<?php
02.$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
03.arsort($fruits);
04.var_dump($fruits);
05.?>
06.结果
07.array
08.'a' => string 'orange' (length=6)
09.'d' => string 'lemon' (length=5)
10.'b' => string 'banana' (length=6)
11.'c' => string 'apple' (length=5)

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

01.<?php$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
02.ksort($fruits);
03.var_dump($fruits);
04.?>
05.结果
06.array
07.'a' => string 'orange' (length=6)
08.'b' => string 'banana' (length=6)
09.'c' => string 'apple' (length=5)
10.'d' => string 'lemon' (length=5)

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

01.<?php
02.$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
03.krsort($fruits);
04.var_dump($fruits);
05.?>
06.结果
07.array
08.'d' => string 'lemon' (length=5)
09.'c' => string 'apple' (length=5)
10.'b' => string 'banana' (length=6)
11.'a' => string 'orange' (length=6)

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

01.<?php
02.function cmp($a, $b)
03.{
04.if ($a == $b) {
05.return 0;
06.}
07.return ($a < $b) ? -1 : 1;
08.}
09. 
10.$a = array(3, 2, 5, 6, 1);
11. 
12.usort($a, "cmp");
13. 
14.var_dump($a);
15.?>
16.结果:
17.array
18.0 => int 1
19.1 => int 2
20.2 => int 3
21.3 => int 5
22.4 => int 6

-----------------uksort使用自定义函数按照数组的key排序-----------------

01.<?php
02.function cmp($a, $b)
03.{
04.$a = preg_replace('@^(a|an|the) @', '', $a);
05.$b = preg_replace('@^(a|an|the) @', '', $b);
06.return strcasecmp($a, $b);
07.}
08. 
09.$a = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
10. 
11.uksort($a, "cmp");
12. 
13.var_dump($a);
14.?>
15.结果:
16.array
17.'an apple' => int 3
18.'a banana' => int 4
19.'the Earth' => int 2
20.'John' => int 1

-------------------uasort将数组用自定义函数按照value排序,保持索引关系不变---------

01.<?php
02.// Comparison function
03.function cmp($a, $b) {
04.if ($a == $b) {
05.return 0;
06.}
07.return ($a < $b) ? -1 : 1;
08.}
09. 
10.// Array to be sorted
11.$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
12.var_dump($array);
13. 
14.// Sort and print the resulting array
15.uasort($array, 'cmp');
16.var_dump($array);
17.?>
18.结果:
19.array
20.'a' => int 4
21.'b' => int 8
22.'c' => int -1
23.'d' => int -9
24.'e' => int 2
25.'f' => int 5
26.'g' => int 3
27.'h' => int -4
28.array
29.'d' => int -9
30.'h' => int -4
31.'c' => int -1
32.'e' => int 2
33.'g' => int 3
34.'a' => int 4
35.'f' => int 5
36.'b' => int 8

-------------------array_multisort排序多个数组或多维数组---------

01.<?php
02.$ar = array(
03.array("10", 11, 100, 100, "a"),
04.array( 1, 2, "2", 3, 1)
05.);
06. 
07.array_multisort($ar[0], SORT_ASC, SORT_STRING,
08.$ar[1], SORT_NUMERIC, SORT_DESC);
09.var_dump($ar);
10.?>
11.结果:
12.array
13.0 =>
14.array
15.0 => string '10' (length=2)
16.1 => int 100
17.2 => int 100
18.3 => int 11
19.4 => string 'a' (length=1)
20.1 =>
21.array
22.0 => int 1
23.1 => int 3
24.2 => string '2' (length=1)
25.3 => int 2
26.4 => int 1

//说明:

1 上例中:$ar数组优先按照$ar[0]的字符串值升序排列,如果字符串值相等,再按照$ar[1]数组的数字值降序排列。

2 array_multisort函数的任意一个位置的参数如果是数组,表示排序时用的值,

如果有多个数组参数,优先按照前边的数组值进行排序,如果是常量,例如

SORT_ASC, SORT_DESC, SORT_REGULAR,SORT_NUMERIC, SORT_STRING.

表示排序方法(数组取值前优先)。

弘智主页 | 弘智介绍 | 培训课程 | 最新开课| 盐城电脑培训 | 人才工作