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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法