发布:net/PHP编程 编辑:thebaby 2013-06-28 13:12:54 【大 中 小】
继上一篇文章:PHP二维数组排序自定义函数,今天,我们再介绍一个php二维数组排序的例子。
php对二维数组的排序很简单,主要用到array_multisort函数。
例子:
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);
|
15 |
foreach ( $data as $key => $row )
|
17 |
$volume [ $key ] = $row [ 'volume' ];
|
18 |
$edition [ $key ] = $row [ 'edition' ];
|
21 |
array_multisort ( $volume , SORT_DESC, $edition , SORT_ASC, $data );
|
输出结果:
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数组排序方法大全 <<<