您的位置:首页 > 编程语言 > PHP开发

代码分享:php对二维数组进行排序

2013-12-19 06:48 441 查看
发布:net/PHP编程 编辑:thebaby 2013-06-28 13:12:54 【大 中 小】
转自:http://www.jbxue.com/article/9991.html
本文介绍下,php中使用array_multisort函数进行二维数组排序的例子,有需要的朋友,参考下吧。

继上一篇文章:PHP二维数组排序自定义函数,今天,我们再介绍一个php二维数组排序的例子。

php对二维数组的排序很简单,主要用到array_multisort函数。

例子:

01
<?php
02
/**
03
* php二维数组排序
04
* edit www.jbxue.com
05
*/
06
$data
=
array
();
07
$data
[] =
array
(
'volume'
=>67,
'edition'
=>2);
08
$data
[] =
array
(
'volume'
=>86,
'edition'
=>1);
09
$data
[] =
array
(
'volume'
=>85,
'edition'
=>6);
10
$data
[] =
array
(
'volume'
=>98,
'edition'
=>2);
11
$data
[] =
array
(
'volume'
=>86,
'edition'
=>6);
12
$data
[] =
array
(
'volume'
=>67,
'edition'
=>7);
13
14
// 取得列的列表
15
foreach
(
$data
as
$key
=>
$row
)
16
{
17
$volume
[
$key
]  =
$row
[
'volume'
];
18
$edition
[
$key
] =
$row
[
'edition'
];
19
}
20
21
array_multisort
(
$volume
, SORT_DESC,
$edition
, SORT_ASC,
$data
);
22
23
print_r(
$data
);
24
?>
输出结果:

Array

(

[0] =>Array

(

[volume] =>98

[edition] =>2

)

[1] =>Array

(

[volume] =>86

[edition] =>1

)

[2] =>Array

(

[volume] =>86

[edition] =>6

)

[3] =>Array

(

[volume] =>85

[edition] =>6

)

[4] =>Array

(

[volume] =>67

[edition] =>2

)

[5] =>Array

(

[volume] =>67

[edition] =>7

)

)

说明:

array_multisort函数的参数非常灵活,大家可以参照php手册中的说明,深入研究下。

>>> 更多内容,请查看 php数组排序方法大全 <<<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: