I have a very specific problem here. I have a multi-dimensional array that I want to sort first by half-hour time intervals, second by dates. The function array_multisort will not meet my requirements.
Example: I want:
array(array("time"=>"12:15",
"date"=>"2009-03-24"),
array("time"=>"12:10",
"date"=>"2009-03-23"),
array("time"=>"12:00",
"date"=>"2009-03-24"),
array("time"=>"11:30",
"date"=>"2009-03-24"));
To end up as:
array(array("time"=>"11:30",
"date"=>"2009-03-24"),
array("time"=>"12:10",
"date"=>"2009-03-23"),
array("time"=>"12:00",
"date"=>"2009-03-24"),
array("time"=>"12:15",
"date"=>"2009-03-24"));
I've tried to accomplish this with uksort in combination with my own sorting callback function. This is the code I am currently using:
uksort($myArray, "sortThirties");
function sortThirties($a, $b)
{
//Get the two times as timestamps relative to today
$one = strtotime($a['time']);
$two = strtotime($b['time']);
//Round them down to the nearest half-hour time
$one = $one - ($one % 1800);
$two = $two - ($two % 1800);
//Return the difference if times are unequal
//If times are equal, return the difference between dates.
return ($one == $two ? strcmp($a['date'],$b['date']) : $one - $two);
}
Immediately after running this function, I print out the array with print_r(), and the order of the data appears to be random. What am I missing?
EDIT: It turns out, the order is completely random. I added this line to the sortThirties function:
echo "<BR>",$a['time']," ",$b['time'];
and all I got was 50 pages of <BR>
's.
I know the array is correctly structured because this code executed on the very same array gives me the unsorted data:
foreach($myArray AS $a)
{
echo "<BR>",$a['date']," ",$a['time'];
}
The only thing I can think of is there must be a problem with uksort. Any thoughts?