I am trying to sort the arrays (each containing 2 date values) in a multidimensional array. I was able to find a useful function which solves the question for one element, however I was unable to modify it for two.
PHP Sort a multidimensional array by element containing date
function date_compare($a, $b)
{
$t1 = strtotime($a['datetime']);
$t2 = strtotime($b['datetime']);
return $t1 - $t2;
}
usort($array, 'date_compare');
The problem at hand is sorting comments in which have a post time and an edit time. Essentially I want to sort them from newest to oldest (while keeping both values).
If this is not possible, let me know.
EDIT: Mockup
$array = array(
[0] => array(
[0] => "Aug:1:2012 12:00:pm", // post date
[1] => "Aug:28:2012 12:00:pm" // edit date
),
[1] => array(
[0] => "Aug:1:2012 12:00:pm",
[1] => "Aug:30:2012 12:00:pm"
)
[2] => array(
[0] => "Aug:29:2012 12:00:pm",
[1] => "Aug:1:2012 12:00:pm"
)
};
Should output: $array[1] first (because it has the highest date out of keys 1 & 2) then $array[2], then $array[0].
$array = array(
[0] => array(
[0] => "Aug:1:2012 12:00:pm",
[1] => "Aug:30:2012 12:00:pm" // highest
),
[1] => array(
[0] => "Aug:29:2012 12:00:pm", // next
[1] => "Aug:1:2012 12:00:pm"
)
[2] => array(
[0] => "Aug:1:2012 12:00:pm",
[1] => "Aug:28:2012 12:00:pm" // lowest
)
};