dongyi1996 2015-01-06 23:07
浏览 32
已采纳

按日期键按子数组排序PHP数组

I know this question has been asked a hundred times, and I've gone through multiple answers and am not getting the correct results.

I am trying to order the following row by date, oldest first:

Array(
[0] => Array
    (
        [0] => '3173'
        [1] => 'Contact - 12-6-14 Outstanding invoice'
        [2] => '16/06/2014'
        [3] => '204'
    )

[1] => Array
    (
        [0] => '3167'
        [1] => 'Contact - Outstanding invoice'
        [2] => '13/06/2014'
        [3] => '207'
    )

[2] => Array
    (
        [0] => '3497'
        [1] => 'New Site - Keri Keri'
        [2] => '25/11/2014'
        [3] => '43'
    )

[3] => Array
    (
        [0] => '2023'
        [1] => 'Analysis'
        [2] => '17/06/2014'
        [3] => '355'
    )

[4] => Array
    (
        [0] => '2641'
        [1] => 'PSS'
        [2] => '20/02/2014'
        [3] => '321'
    )

)

I have tried things such as the below with no luck.

function cmp($a, $b){
      return $b[2] - $a[2];
   } 

usort($urgent_array, "cmp");

Any help on this one would be really appreciated :)

  • 写回答

2条回答 默认 最新

  • doufenyu7610 2015-01-06 23:15
    关注

    First, you want to compare, not subtract:

    function cmp($a, $b) {
        if ($a[2] == $b[2])
            return 0;
        return ($a[2] > $b[2]) ? 1 : -1;
    } 
    
    usort($urgent_array, "cmp");
    

    Then, date ordering works best if you use the Y-m-d format:

    2014-06-13 etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?