zoukankan      html  css  js  c++  java
  • 代码分享:php对二维数组进行排序

    发布: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数组排序方法大全 <<<

  • 相关阅读:
    LeetCode题解之Flipping an Image
    LeetCode 之Find Minimum in Rotated Sorted Array
    LeetCode题解Transpose Matrix
    LeetCode 题解之Minimum Index Sum of Two Lists
    LeetCode题解之Intersection of Two Linked Lists
    LeetCode 题解之Add Two Numbers II
    LeetCode题解之Add two numbers
    href="#"与href="javascript:void(0)"的区别
    有关ie9 以下不支持placeholder属性以及获得焦点placeholder的移除
    ie7下属性书写不规范造成的easyui 弹窗布局紊乱
  • 原文地址:https://www.cnblogs.com/linuxnotes/p/3481396.html
Copyright © 2011-2022 走看看