萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP數組排序函數合集 以及它們之間的聯系分析

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
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// 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排序多個數組或多維數組---------

復制代碼 代碼如下:
<?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
//說明:

copyright © 萬盛學電腦網 all rights reserved