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,
    ),
  ),
);
4
  • 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 Commented Oct 12, 2015 at 8:15
  • 1
    Possible duplicate of PHP Sort a multidimensional array by element containing date Commented Oct 12, 2015 at 8:25
  • @syck Not exactly duplicate. OP has dates as keys, but your suggested question has dates as array value item. But close. Commented Oct 12, 2015 at 8:28
  • Related: Sort array of objects by date field Commented Aug 28, 2022 at 23:41

1 Answer 1

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
                )

        )

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