I have a non-associative array in which the data that arrives is not sorted (I get data from an external system and cannot force it to enter the array in sorted order.) Is there a way to sort the values? I tried this:
$wedTrackTimes = array("9:30 AM-10:30 AM", "8:15 AM-9:15 AM", "12:30 PM-1:30 PM", "2:00 PM-3:00 PM", "3:30 PM-4:30 PM"); $wedTrackTimes = array_unique($wedTrackTimes); $wedTrackTimes = sort($wedTrackTimes); print_r($wedTrackTimes);
But instead of returning a sorted array, it returns 1. I assume that it is not associative, so there are no keys. Is there a way to sort an array by value only? We really need the 9:30 AM slot, which will drop out after the 8:15 AM slot, as it should be.
UPDATE
Thanks to everyone for the answers; which did sort the array, but not as expected. If I use the default sort type, I get the following:
Array ( [0] => 12:30 PM-1:30 PM [1] => 2:00 PM-3:00 PM [2] => 3:30 PM-4:30 PM [3] => 8:15 AM-9:15 AM [4] => 9:30 AM-10:30 AM )
Using SORT_NUMERIC I get the following:
Array ( [0] => 2:00 PM-3:00 PM [1] => 3:30 PM-4:30 PM [2] => 8:15 AM-9:15 AM [3] => 9:30 AM-10:30 AM [4] => 12:30 PM-1:30 PM )
With SORT_STRING, I get the following:
Array ( [0] => 12:30 PM-1:30 PM [1] => 2:00 PM-3:00 PM [2] => 3:30 PM-4:30 PM [3] => 8:15 AM-9:15 AM [4] => 9:30 AM-10:30 AM )
What I need:
Array ( [0] => 8:15 AM-9:15 AM [1] => 9:30 AM-10:30 AM [2] => 12:30 PM-1:30 PM [3] => 2:00 PM-3:00 PM [4] => 3:30 PM-4:30 PM )
Is it possible?
sorting arrays php time
EmmyS
source share