Reputation: 228
Does anybody have an idea how can I sort this array by key (date) in PHP?
Array
(
[2011-02-16] => Array
(
[date] => 2011-02-16
[num] => 2
)
[2011-02-11] => Array
(
[date] => 2011-02-11
[num] => 0
)
[2011-02-17] => Array
(
[date] => 2011-02-17
[num] => 0
)
[2011-02-18] => Array
(
[date] => 2011-02-18
[num] => 0
)
)
Upvotes: 2
Views: 481
Reputation: 1780
A better answer would be to use uksort
which is used to sort keys with a user-defined function (considering that these dates cannot always be compared and cannot be sorted with ksort
without first applying strtotime
to the keys):
function sort_by_date_keys($date_key1, $date_key2) {
// reverse the order of the $date_keys for "oldest to newest"
return strtotime($date_key2) - strtotime($date_key1);
);
uksort($array, 'sort_by_date_keys');
This method is more defined than uasort
as it was tailored for keys.
Example:
$array = array(
'1/1/12' => 'foo1',
'1/1/13' => 'foo2'
);
uksort($array, 'sort_by_date_keys');
// output
$array = array(
'1/1/13' => 'foo2',
'1/1/12' => 'foo1'
);
Upvotes: 0
Reputation: 3479
What you want is UASORT.
https://www.php.net/manual/en/function.uasort.php
function would be like:
function cmp($a, $b) {
$d1 = strtotime($a['date']);
$d2 = strtotime($b['date']);
if ($d1 == $d2) {
return 0;
}
return ($d1 < $d2) ? -1 : 1;
}
Upvotes: 0
Reputation: 8647
http://www.php.net/manual/en/function.array-multisort.php
Would example #3 suit your needs?
Upvotes: 0
Reputation: 17314
Since your array already has the date in the keys, why not just use ksort
? The string comparison should work fine since you're using YYYY-MM-dd format.
Upvotes: 0
Reputation: 6973
have you tried ksort? http://www.php.net/manual/en/function.ksort.php
Upvotes: 2
Reputation: 300845
Well, as sorting on the key would do it, ksort() would work for you!
But if you really want to sort on the date element, you would use uasort() with a sort function like this
function compare($a, $b)
{
if ($a['date']==$b['date']) return 0;
return ($a['date']>$b['date'])?1:-1;
}
uasort($myarray, 'compare');
Upvotes: 0
Reputation: 18002
This should help you brush up on the basics of array sorting in PHP
http://www.the-art-of-web.com/php/sortarray/
Something like this would sort your problem however:
usort($array, "cmp");
function cmp($a, $b){
return strcmp($b['date'], $a['date']);
Upvotes: 0
Reputation: 29160
Sure thing. I answered this exact post yesterday, too.
Sorting 2 arrays to get the highest in one and lowest in another
In your case, it will be something like...
$myArray= subval_sort($myArray,'date');
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val){
$c[] = $a[$key];
}
return $c;
}
EDIT
The answer by shamittomar
is better though :)
Upvotes: 0
Reputation: 46692
Use the uasort
function, which is user customizable sorting. Like this:
function cmp($a, $b)
{
if ($a['date'] == $b['date'])
{
return 0;
}
return ($a['date'] < $b['date']) ? -1 : 1;
}
uasort($your_array, "cmp");
Upvotes: 7