函数的排序有一些共性:
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)
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","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)
$fruits = array("lemon","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)
$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","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)
$fruits = array("d" => "lemon","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)
$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","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)
$fruits = array("d"=>"lemon","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);
?>
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);
?>
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);
?>
// 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,"a"),
array( 1,"2",3,1)
);array_multisort($ar[0],SORT_ASC,SORT_STRING,
$ar[1],SORT_NUMERIC,SORT_DESC);
var_dump($ar);
?>
$ar = array(
array("10",11,100,"a"),
array( 1,"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_STRING.
表示排序方法(数组取值前优先)。
==========================================================================================
PHP二维数组排序函数
PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义。以下函数是对一个给定的二维数组按照指定的键值进行排序,先看函数定义:
代码如下:
function array_sort($arr,$keys,$type='asc'){
$keysvalue = $new_array = array();
foreach ($arr as $k=>$v){
$keysvalue[$k] = $v[$keys];
}
if($type == 'asc'){
asort($keysvalue);
}else{
arsort($keysvalue);
}
reset($keysvalue);
foreach ($keysvalue as $k=>$v){
$new_array[$k] = $arr[$k];
}
return $new_array;
}
$keysvalue = $new_array = array();
foreach ($arr as $k=>$v){
$keysvalue[$k] = $v[$keys];
}
if($type == 'asc'){
asort($keysvalue);
}else{
arsort($keysvalue);
}
reset($keysvalue);
foreach ($keysvalue as $k=>$v){
$new_array[$k] = $arr[$k];
}
return $new_array;
}
它可以对二维数组按照指定的键值进行排序,也可以指定升序或降序排序法(默认为升序),用法示例:
代码如下:
$array = array(
array('name'=>'手机','brand'=>'诺基亚','price'=>1050),
array('name'=>'笔记本电脑','brand'=>'lenovo','price'=>4300),
array('name'=>'剃须刀','brand'=>'飞利浦','price'=>3100),
array('name'=>'跑步机','brand'=>'三和松石','price'=>4900),
array('name'=>'手表','brand'=>'卡西欧','price'=>960),
array('name'=>'液晶电视','brand'=>'索尼','price'=>6299),
array('name'=>'激光打印机','brand'=>'惠普','price'=>1200)
);
$ShoppingList = array_sort($array,'price');
print_r($ShoppingList);
array('name'=>'手机','brand'=>'诺基亚','price'=>1050),
array('name'=>'笔记本电脑','brand'=>'lenovo','price'=>4300),
array('name'=>'剃须刀','brand'=>'飞利浦','price'=>3100),
array('name'=>'跑步机','brand'=>'三和松石','price'=>4900),
array('name'=>'手表','brand'=>'卡西欧','price'=>960),
array('name'=>'液晶电视','brand'=>'索尼','price'=>6299),
array('name'=>'激光打印机','brand'=>'惠普','price'=>1200)
);
$ShoppingList = array_sort($array,'price');
print_r($ShoppingList);
上面是对$array这个二维数组按照'price'从低到高的排序。
输出结果:(略)。