0

I want to sort the array without losing the keys.I read many similar questions here on stackoverflow but none of them deals with date as key. Hope someone can help me!

Here is the array that I want to sort.

$data = array (
  'October 8, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 1,
      'checkout' => 0,
    ),
  ),
  'September 8, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 1,
      'checkout' => 0,
    ),
  ),
  'September 7, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 1,
      'checkout' => 1,
    ),
  ),
  'September 6, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 2,
      'checkout' => 2,
    ),
  ),
  'September 5, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 1,
      'checkout' => 1,
    ),
  ),
  'September 4, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 1,
      'checkout' => 1,
    ),
  ),
  'September 3, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 2,
      'checkout' => 2,
    ),
  ),
  'September 2, 2015' => 
  array (
    'admin' => 
    array (
      'checkin' => 2,
      'checkout' => 1,
    ),
  ),
);
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163
user3407278
  • 1,233
  • 5
  • 16
  • 32
  • Are you wanting to sort in date order or lexicographical order? If in date order, you will need to create a second array converting the keys to something like a unix timestamp, then sort by that. Or convert the date string keys to unix timestamps and move the date string into the value of the array – Gravy Oct 12 '15 at 08:15
  • 1
    Possible duplicate of [PHP Sort a multidimensional array by element containing date](http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date) – syck Oct 12 '15 at 08:25
  • @syck Not exactly duplicate. OP has dates as keys, but your suggested question has dates as array value item. But close. – Rene Korss Oct 12 '15 at 08:28
  • Related: [Sort array of objects by date field](https://stackoverflow.com/q/7127764/2943403) – mickmackusa Aug 28 '22 at 23:41

1 Answers1

1

Use uksort. You can make your own compare function. There you could convert key into timestamp and compare.

You can change order direction

return $a > $b; // order ASC
return $a < $b; // order DESC  

Your code should look like:

function cmp($a, $b)
{
    $a = strtotime($a);
    $b = strtotime($b);
    return $a > $b;
}

uksort($data, "cmp");

See DEMO

OUTPUT

Array
(
    [September 2, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 2
                    [checkout] => 1
                )

        )

    [September 3, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 2
                    [checkout] => 2
                )

        )

    [September 4, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 1
                    [checkout] => 1
                )

        )

    [September 5, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 1
                    [checkout] => 1
                )

        )

    [September 6, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 2
                    [checkout] => 2
                )

        )

    [September 7, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 1
                    [checkout] => 1
                )

        )

    [September 8, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 1
                    [checkout] => 0
                )

        )

    [October 8, 2015] => Array
        (
            [admin] => Array
                (
                    [checkin] => 1
                    [checkout] => 0
                )

        )

)
Rene Korss
  • 5,414
  • 3
  • 31
  • 38