PrimuS PrimuS - 4 months ago 14
PHP Question

How to sort Array with date as key?

I have an array like this:

array:6 [▼
"2016-07-28" => array:18 [▶]
"2016-07-29" => array:18 [▶]
"2016-07-30" => array:5 [▶]
"2016-07-31" => array:5 [▶]
"2016-08-01" => array:17 [▶]
"2016-08-02" => array:11 [▶]
]


I know want to revert the order, having
2016-08-02
the first and
2016-07-28
the last entry.

I tried

asort($missingArr);


Result

array:6 [▼
"2016-07-29" => array:18 [▶]
"2016-07-28" => array:18 [▶]
"2016-08-01" => array:17 [▶]
"2016-08-02" => array:11 [▶]
"2016-07-31" => array:5 [▶]
"2016-07-30" => array:5 [▶]
]


Even when converting the date to a unix timestamp it does the exact same thing. Where am I wrong?

Answer

With krsort (k stands for "key", r for "reverse"):

krsort($missingArr);