Home > Backend Development > PHP Tutorial > php对二维数组进行排序的简单实例_php实例

php对二维数组进行排序的简单实例_php实例

WBOY
Release: 2016-06-07 17:22:50
Original
682 people have browsed it

本文介绍下,php中使用array_multisort函数进行二维数组排序的例子,有需要的朋友,参考下吧。继上一篇文章:PHP二维数组排序自定义函数,今天,我们再介绍一个php二维数组排序的例子。
php对二维数组的排序很简单,主要用到array_multisort函数。
例子:

复制代码 代码如下:

/**
* php二维数组排序
* edit www.php.net
*/
    $data = array();
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);
    // 取得列的列表
    foreach ($data as $key => $row)
    {
        $volume[$key]  = $row['volume'];
        $edition[$key] = $row['edition'];
    }
    array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
    print_r($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手册中的说明,深入研究下。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template