1.array_multisort()
函数对多个数组或多维数组进行排序。
//对数组$hotcat按照count键值大小降序进行排序;
$hotcat
=
array
(
array
(
‘1501‘
=>
array
(
‘catid‘
=>
‘1546‘
,
‘catname‘
=>
‘数组排序 一级‘
,
‘count‘
=>
‘588‘
)),
array
(
‘1501‘
=>
array
(
‘catid‘
=>
‘1546‘
,
‘catname‘
=>
‘数组排序二级‘
,
‘count‘
=>
‘588‘
)),
array
(
‘1501‘
=>
array
(
‘catid‘
=>
‘1546‘
,
‘catname‘
=>
‘数组排序 三级‘
,
‘count‘
=>
‘588‘
))
);
//提取列数组;
foreach
(
$hotcat
as
$key
=>
$val
) {
$tmp
[
$key
] =
$row
[
‘username‘
];
}
array_multisort
(
$tmp
,SORT_DESC,
$hotcat
);此处对数组进行降序排列;SORT_DESC按降序排列
2.自定义方法array_sort()
进行排序
//指定数组以$keys键值排序
function
array_sort(
$array
,
$keys
,
$type
=
‘asc‘
){
//$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序
$keysvalue
=
$new_array
=
array
();
foreach
(
$array
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
] =
$array
[
$k
];
}
return
$new_array
;
}
转自:https://www.jb51.net/article/112285.htm(感谢分享)
原文地址:https://www.cnblogs.com/luqiang213917/p/9367560.html