按日期排序数组并维护索引关联

时间:2012-11-26 23:31:10

标签: php arrays sorting

在php中有没有办法在哪里可以按日期对数组进行排序并显示它们? 类似于asort或usort?

问题是日期字段也可以包含“UNKNOWN”之类的值。 我希望按日期降序打印所有记录,最后打印UNKNOWNS。  到目前为止,这是我的代码。

foreach ($MailPieceArr as $key => $val) {

    $count++;
    if (($count % 2) == 1) {
        print "<tr bgcolor='#CCCCCC'>";
    } else {
        print "<tr bgcolor=white>";
    }

    print "<td align=center>".$MailPieceArr[$key]["type"]."</td>";
    print "<td align=center>".$MailPieceArr[$key]["id"]."</td>";
    print "<td align=center>".$MailPieceArr[$key]["ddate"]."</td>";
    print "<td align=center>".$MailPieceArr[$key]["acode"]."</td>";

    print "</tr>";
}

以下是我的数组的样子:

print_r($MailPieceArr);

输出:

Array
(
    [0] => Array
        (
            [type] => First-Class
            [id] => 77770056
            [ddate] => 11/26/2012
            [acode] => 2643
        )

    [1] => Array
        (
            [type] => First-Class
            [id] => 77770057
            [ddate] => 11/26/2012
            [acode] => 2643
        )

    [2] => Array
        (
            [type] => First-Class
            [id] => 77770058
            [ddate] => 11/21/2012
            [acode] => 2641
        )

    [3] => Array
        (
            [type] => First-Class
            [id] => 77770059
            [ddate] => 11/21/2012
            [acode] => 2641
        )

    [4] => Array
        (
            [type] => First-Class
            [id] => 77770042
            [ddate] => 10/29/2012
            [acode] => 2623
        )

    [5] => Array
        (
            [type] => First-Class
            [id] => 77770041
            [ddate] => 10/29/2012
            [acode] => 2623
        )

    [6] => Array
        (
            [type] => RR
            [id] => 88880001
            [ddate] => 10/29/2012
            [acode] => 2621
        )

    [7] => Array
        (
            [type] => First-Class
            [id] => 77770043
            [ddate] => 10/17/2012
            [acode] => 2625
        )

    [8] => Array
        (
            [type] => First-Class
            [id] => 77770044
            [ddate] => 10/17/2012
            [acode] => 2625
        )

    [9] => Array
        (
            [type] => RR
            [id] => 88880005
            [ddate] => 10/17/2012
            [acode] => 2619
        )

    [10] => Array
        (
            [type] => IMB
            [id] => 0001101111111111110011111111111
            [ddate] => UNKNOWN   
            [acode] => 2643
        )

)

3 个答案:

答案 0 :(得分:1)

在排序函数中使用strtotime,不仅比较时间戳更容易,而且在无效日期格式上也返回false。

function date_sort($a, $b)
{
    $t1 = strtotime($a['ddate']);
    $t2 = strtotime($b['ddate']);

    if($t1 == false || $t2 == false)
        return 1;

    return $t2 - $t1;
}    
usort($array, 'date_sort');

这可以帮助您生成类似this example的内容。

答案 1 :(得分:0)

您可以将日期转换为unix时间戳,它们将更容易比较。

答案 2 :(得分:0)

使用usort()可以使用完全自定义的比较函数对数组进行排序:

<?php
date_default_timezone_set('UTC');

$array  = array(
    array(
        'type'  => 'First-Class',
        'id'    => 7770057,
        'ddate' => '2012/11/26',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770058,
        'ddate' => '2012/11/21',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770059,
        'ddate' => 'UNKNOWN',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770060,
        'ddate' => 'UNKNOWN',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770061,
        'ddate' => '2012/11/30',
        'acode' => 2643
    ),
);


echo 'Before:'.PHP_EOL;
foreach($array as $item){
    echo $item['id'].': '.$item['ddate'].PHP_EOL;
}

usort($array, function($a, $b){
    if($a['ddate'] == 'UNKNOWN'){
            // Place UNKNOWN at end
        return 1;
    }

    if($a['ddate'] == $b['ddate']){
            // Dates are the same
        return 0;
    }
    return (strtotime($a['ddate']) > strtotime($b['ddate'])) ? -1 : 1;
});

echo 'After:'.PHP_EOL;
foreach($array as $item){
    echo $item['id'].': '.$item['ddate'].PHP_EOL;
}

但是,您应该考虑使用PHP的DateTime class来存储日期对象。使用字符串可能含糊不清 - 例如,12月11日或11月12日是2012年12月11日?