php多维数组按日期排序

时间:2017-08-31 18:22:29

标签: php arrays sorting multidimensional-array

我有一个多维数组,其中包含具有日期字段的数据。某些索引可能有单个日期字段,其他索引可能有多个。下面列出了具有多个日期的索引的示例。

Array ( [0] => Array ( 
    [0] => Array ()
... ... ... 
    [21] => Array ( 
       [0] => Array ( 
          [Date] => 2011-05-12
          [Color] => green
          [State] => Ohio
        )
       [1] => Array ( 
         [Date] => 1999-01-23
         [Color] => red
         [State] => Vermont
        )
       [2] => Array ( 
         [Date] => 3001-08-24
         [Color] => yellow
         [State] => Alaska
        )

我正在尝试将它们列为最新日期,成为最早进入最早条目的第一个条目。例如

 Array ( [0] => Array ( 
    [0] => Array ()
... ... ... 
    [21] => Array (
       [0] => Array ( 
         [Date] => 3001-08-24
         [Color] => yellow
         [State] => Alaska
        )
        [1] => Array ( 
          [Date] => 2011-05-12
          [Color] => green
          [State] => Ohio
        )
        [2] => Array ( 
         [Date] => 1999-01-23
         [Color] => red
         [State] => Vermont
        )

我试过了

function date_compare($a, $b){
    $t1 = strtotime($a["Date"]);
    $t2 = strtotime($b["Date"]);
    return $t2 - $t1;
}

我收到错误

Notice: Undefined index: Date in date_compare() for both lines of $t1 and $t2

当我把

function date_compare($a, $b){
    $t1 = strtotime($a[0]["Date"]);
    $t2 = strtotime($b[0]["Date"]);
    return $t2 - $t1;
}

我收到错误

Notice: Undefined offset: 0 in date_compare(). On the second line $t2.

注意:数组以[0]索引开始,然后进入[0],[1],[2]。

1 个答案:

答案 0 :(得分:2)

首先循环数组并使用usort对子数组进行排序。例如,将数组视为

$array = array(array(
            array('Date' => '1999-01-23','Color' => 'red','State' => 'Vermont'),
            array('Date' => '3001-08-24','Color' => 'yellow','State' => 'Alaska'),
            array('Date' => '2011-05-12','Color' => 'green','State' => 'Ohio'),
            ),
            array(
            array('Date' => '2017-01-23','Color' => 'red','State' => 'Vermont'),
            array('Date' => '2017-08-24','Color' => 'yellow','State' => 'Alaska'),
            array('Date' => '2000-05-12','Color' => 'green','State' => 'Ohio'),
            )
       );

排序代码:

function date_compare($a, $b)
{
    $t1 = strtotime($a['Date']);
    $t2 = strtotime($b['Date']);
    return $t2 - $t1;  // descending
}  
  $sorted_array = array();
// loop the array
foreach($array as $key=>$value){
    usort($value, 'date_compare'); // sort the array
    $sorted_array[$key] = $value;  // assign sorted array to new array
}

print_r($sorted_array);

输出

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Date] => 3001-08-24
                    [Color] => yellow
                    [State] => Alaska
                )

            [1] => Array
                (
                    [Date] => 2011-05-12
                    [Color] => green
                    [State] => Ohio
                )

            [2] => Array
                (
                    [Date] => 1999-01-23
                    [Color] => red
                    [State] => Vermont
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Date] => 2017-08-24
                    [Color] => yellow
                    [State] => Alaska
                )

            [1] => Array
                (
                    [Date] => 2017-01-23
                    [Color] => red
                    [State] => Vermont
                )

            [2] => Array
                (
                    [Date] => 200-05-12
                    [Color] => green
                    [State] => Ohio
                )

        )

)

http://sandbox.onlinephpfunctions.com/code/631c4b904d937ad181ccff20cbd3fa89c697f06b