0

Array keys are date and want to sort array by date in Asending order. Following is array:

Array
(
    [07/14/2017] => Array
        (
            [ID] => 5442
            [post_content] => Test1
            [post_title] => Testevents1
        )

    [01/11/2017] => Array
        (
            [ID] => 5443
            [post_content] => Test2
            [post_title] => Testevents2
        )
)
RD Mage
  • 103
  • 3
  • 16
  • its already answered [here](https://stackoverflow.com/questions/38770210/how-to-sort-array-with-date-as-key) with more explaination [ here](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – Ahmed Sunny Jul 07 '17 at 10:28
  • Here is your answer [answer](https://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date) – always-a-learner Jul 07 '17 at 10:31

2 Answers2

2

You can use uksort to do that:

uksort($arr, function ($a, $b) {
    $t1 = strtotime($a);
    $t2 = strtotime($b);
    if ($t1 == $t2) {
        return 0;
    }
    return ($t1 > $t2) ? 1 : -1;
});
Razon Yang
  • 420
  • 3
  • 10
0

You can use uksort for this.

uksort — Sort an array by keys using a user-defined comparison function

function cmp($keyA, $keyB)
{
    // Your date parsing and comparison

    // The comparison function must return an integer less than, equal to, 
    // or greater than zero if the first argument is considered to be respectively 
    // less than, equal to, or greater than the second. 
    // Note that before PHP 7.0.0 this integer had to be in the range 
    // from -2147483648 to 2147483647.
}

uksort($arr, "cmp")
Artur K.
  • 3,263
  • 3
  • 38
  • 54