Answer the question
In order to leave comments, you need to log in
How to sort array under date as mY?
There are such arrays:
array(29) {
["03.2020"]=>
array(4) {
["likes"]=>
int(46003)
["comments"]=>
int(2663)
["reposts"]=>
int(2939)
["views"]=>
int(3386015)
}
["01.2020"]=>
array(4) {
["likes"]=>
int(8958)
["comments"]=>
int(612)
["reposts"]=>
int(404)
["views"]=>
int(699785)
}
["02.2020"]=>
array(4) {
["likes"]=>
int(63776)
["comments"]=>
int(3298)
["reposts"]=>
int(3218)
["views"]=>
int(4411204)
}
["01.2019"]=>
array(4) {
["likes"]=>
int(33402)
["comments"]=>
int(2037)
["reposts"]=>
int(1096)
["views"]=>
int(1236658)
}
["12.2019"]=>
array(4) {
["likes"]=>
int(47592)
["comments"]=>
int(1846)
["reposts"]=>
int(832)
["views"]=>
int(1552732)
}
}
Answer the question
In order to leave comments, you need to log in
array_uksort() - Sorts an array by keys, using a custom key comparison function.
Write a function that determines the relationship between two dates in your format (less than, equal to, greater than) and use it in array_uksort.
Buddy, even you are too smart with the code, hold on, it's better this way:
$array = [
'03.2001' => [],
'01.2000' => [],
'01.2005' => [],
'05.2020' => [],
'01.2020' => [],
'30.1998' => [],
'03.2020' => [],
'09.2020' => [],
'04.1998' => [],
'11.2005' => [],
];
uksort($array, function(string $a, string $b): int {
return date_create_from_format('m.Y', $a) <=> date_create_from_format('m.Y', $b);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question