1

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].

1
  • Strtotime won't work on dates that far ahead (as of now). It's somewhere 2038 that is the max number with 32 bit UNIX time. Commented Aug 31, 2017 at 19:12

1 Answer 1

2

First loop the array and sort the sub array using 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.