简体   繁体   English

PHP多维数组按日期排序

[英]php multidimensional array sort by date

I have a multidimensional array which contains data that has date fields. 我有一个多维数组,其中包含具有日期字段的数据。 Some indexes may have single date fields, others may have multiple. 一些索引可能只有一个日期字段,而另一些可能有多个。 An example of an index with multiple date is listed below. 下面列出了具有多个日期的索引的示例。

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
        )

I am trying to list them as the most current date to be the first entry down to the oldest entry. 我试图将它们列为最新日期,成为最早的条目,直到最旧的条目。 For example 例如

 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
        )

I have tried 我努力了

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

I get an error of 我得到一个错误

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

And when I put 当我把

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

I get an error of 我得到一个错误

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

Note: The Array starts off with a [0] index, then it goes into [0], [1], [2]. 注意:数组以[0]索引开始,然后进入[0],[1],[2]。

First loop the array and sort the sub array using usort. 首先循环数组,并使用usort对子数组进行排序。 For example considering the array to be 例如考虑数组为

$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'),
            )
       );

Sorting code: 排序代码:

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);

Output 产量

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 http://sandbox.onlinephpfunctions.com/code/631c4b904d937ad181ccff20cbd3fa89c697f06b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM