Hi I need to sort an array by date:
This is my array $get:
Array (
[0] => Array (
[Date] => 02.06.2012
[Theme] => test
)
[1] => Array (
[Date] => 03.07.2012
[Theme] => lol
)
[2] => Array (
[Date] => 09.06.2012
[Theme] => hm
)
)
I tried following code:
function date_sort($a, $b) {
return strcmp($a['Date'], $b['Date']);
}
usort($get, 'date_sort');
But all I get is unordered dates. My date is set dd.mm.yyyy!
Try doing
function date_sort($a, $b) {
return strtotime($a['Date']) - strtotime($b['Date']);
}
Related
I know this question has been asked a hundred times, and I've gone through multiple answers and am not getting the correct results.
I am trying to order the following row by date, oldest first:
Array(
[0] => Array
(
[0] => '3173'
[1] => 'Contact - 12-6-14 Outstanding invoice'
[2] => '16/06/2014'
[3] => '204'
)
[1] => Array
(
[0] => '3167'
[1] => 'Contact - Outstanding invoice'
[2] => '13/06/2014'
[3] => '207'
)
[2] => Array
(
[0] => '3497'
[1] => 'New Site - Keri Keri'
[2] => '25/11/2014'
[3] => '43'
)
[3] => Array
(
[0] => '2023'
[1] => 'Analysis'
[2] => '17/06/2014'
[3] => '355'
)
[4] => Array
(
[0] => '2641'
[1] => 'PSS'
[2] => '20/02/2014'
[3] => '321'
)
)
I have tried things such as the below with no luck.
function cmp($a, $b){
return $b[2] - $a[2];
}
usort($urgent_array, "cmp");
Any help on this one would be really appreciated :)
First, you want to compare, not subtract:
function cmp($a, $b) {
if ($a[2] == $b[2])
return 0;
return ($a[2] > $b[2]) ? 1 : -1;
}
usort($urgent_array, "cmp");
Then, date ordering works best if you use the Y-m-d format:
2014-06-13 etc.
Change your compare function to
function cmp($a, $b){
$a_date = strtotime(str_replace('/', '-', $a[2]));
$b_date = strtotime(str_replace('/', '-', $b[2]));
return $a_date - $b_date;
}
You want to use strtotime because subtracting strings doesn't really mean anything. The str_replace is because PHP expects dashes for dd-mm-yyyy format.
I need to sort my array php by a key value. my array:
Array
(
[1430039342393636453] => Array
(
[0] => Array
(
[thrid] => 1430039342393636453
[uid] => 19748
[flag] => 1
[timestamp] => 1363791789
[date] => Mar 20
[content_preview] =>
[content] =>
)
)
[1430750471744336569] => Array
(
[0] => Array
(
[thrid] => 1430750471744336569
[uid] => 19870
[flag] => 1
[timestamp] => 1364469959
[date] => Mar 28
[content_preview] =>
[content] =>
)
[1] => Array
(
[thrid] => 1430750471744336569
[uid] => 19874
[flag] => 1
[timestamp] => 1364472417
[date] => Mar 28
[content_preview] =>
[content] =>
)
)
I need to sort by timestamp the main array and also the childs arrays.
Any suggesitons?
Use asort to sort associative arrays.
Related S.O. Post:
Sorting an associative array in PHP
Try something like:
private function sort($sort) {
foreach($sort as &$arr) { //use a reference because usort uses references to manipulate your array.
//if you don't pass by reference you'll never see the sort
usort($arr, function($a, $b) { return $a['timestamp'] - $b['timestamp'];
}
usort($sort, function($a, $b) { return key($a) - key($b); }
return $sort;
}
I've got a multi dimensional array as seen below. I want to sort the 2nd level arrays based the [date] attribute. I believe I can use array_multisort, but I'm unsure of how to proceed.
My array is in the variable $presentations
Array
(
[0] => Array
(
[date] => 20111104
[name] => Name of Presentation
)
[1] => Array
(
[date] => 20111118
[name] => sadf
)
[2] => Array
(
[date] => 20100427
[name] => older one
)
[3] => Array
(
[date] => 20101213
[name] => Another one from 2010
)
[4] => Array
(
[date] => 20110719
[name] => sdf
)
[5] => Array
(
[date] => 20110614
[name] => Sixth one
)
)
A usort callback should return 3 types of values, depending on the circumstances:
A negative number if parameter $a is less than $b
A positive number if parameter $b is less than $a
Zero if both $a and $b are equal
usort($presentations, function($a, $b)
{
if($a['date'] == $b['date'])
{
return 0;
}
return $a['date'] < $b['date'] ? -1 : 1;
});
You can use usort() to apply a custom comparison function.
usort($presentations,
function ($left, $right) {
return $left['date'] - $right['date'];
});
Here's a string implementation which works with integers in PHP because of type juggling:
usort($presentations, function($a, $b) {
return strcmp($a['date'], $b['date']);
});
Array
(
[1] => Array
(
[id] => 1
[sort] => 1
)
[3] => Array
(
[id] => 3
[sort] => 3
)
[2] => Array
(
[id] => 2
[sort] => 2
)
)
How do i sort it so its re-ordered using the inner 'sort' key ? ie the above would look like this:
Array
(
[1] => Array
(
[id] => 1
[sort] => 1
)
[2] => Array
(
[id] => 2
[sort] => 2
)
[3] => Array
(
[id] => 3
[sort] => 3
)
)
You can use usort with this comparison function:
function cmpBySort($a, $b) {
return $a['sort'] - $b['sort'];
}
usort($arr, 'cmpBySort');
Or you use array_multisort with an additional array of key values for the sort order:
$keys = array_map(function($val) { return $val['sort']; }, $arr);
array_multisort($keys, $arr);
Here array_map with the anonymous function is used to build an array of the sort values that is used to sort the array values itself. The advantage of this is that there is np comparison function that needs to be called for each pair of values.
Something like this:
usort($array, function (array $a, array $b) { return $a["sort"] - $b["sort"]; });
Something like this:
uasort($array, 'compfunc');
function compfunc($a, $b)
{
return $a['sort'] - $b['sort'];
}
i have an array like this:
Array
(
[0] => Array
(
[title] => some title
[time] => 1279231500
)
[1] => Array
(
[title] => some title 2
[time] => 1279231440
)
[2] => Array
(
[title] => some title 3
[time] => 1279229880
)
)
how i can sort it based on time?
You can sort it this way (since it is an associative array):
function cmp($a, $b)
{
return strcmp($a['time'], $b['time']);
}
usort($your_array, "cmp");
print_r($your_array);
As Gumbo mentioned, you should not use strcmp for integer values.
Use this function
function cmp($a, $b) {
if ($a['time'] == $b['time'])
return 0;
return ($a['time'] < $b['time']) ? -1 : 1;
}