How to sort an array of times in chronological order?

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?

+8
sorting arrays php time
source share
8 answers

So it looks like you are looking for something more advanced than standard.

 // WARNING: THIS IS *NOT* BY REFERENCE. IT RETURNS A NEW ARRAY. function getSortedTimes(array $group) { $tmp = array(); foreach( $group as $times ) { // Basically, I am pairing the string for the start time with // a numeric value. $tmp[$times] = strtotime(substr($times, 0, strpos($times, '-'))); } // asort is like sort, but it keeps the pairings just created. asort($tmp); // the keys of $tmp now refer to your original times. return array_keys($tmp); } 
+5
source share

Sorting works by reference (this means that it sorts everything that you pass to it), it returns true / false depending on the failure. What are you doing here:

 $wedTrackTimes = sort($wedTrackTimes); 

sets the value of $ wedTrackTimes to TRUE or FALSE.

Try

 sort($wedTrackTimes); print_r($wedTrackTimes); 
+21
source share

That's right, sorting returns a bool . Just use this:

 sort($wedTrackTimes); 
+5
source share

sort , like all php sort functions, sorts in place. It returns true if the sorting was successful, otherwise false. This result does not matter if you only sort strings / numbers.

 $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); sort($wedTrackTimes); print_r($wedTrackTimes); 

- way.

+5
source share

change your code to this:

 $wedTrackTimes = array_unique($wedTrackTimes); sort($wedTrackTimes); print_r($wedTrackTimes); 

as you can read in the documentation , the return value of sort() is true / 1 or false / 0 and indicates whether sorting was possible or an error occurred.

+2
source share

Remove $ wedTrackTimes = before sorting.

  $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); sort($wedTrackTimes); print_r($wedTrackTimes); 
+2
source share

In your code

 $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); 

Do not assign a variable to the sort function, Basically what you do is assign a sort value (which will be true or false) to wedTrackTimes, Use instead

 $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); **sort($wedTrackTimes);** print_r($wedTrackTimes); 
0
source share

Reasonable time sorting (in chronological order) will call strtotime() .

Here is one liner using array_multisort() . array_map() and strtotime() called to create an array that will be used exclusively for sorting order.

 array_multisort(array_map(function($v){return strtotime(strstr($v,'-',true));},$wedTrackTimes),$wedTrackTimes); 

For those who are lost in this syntax, there is the same functionality on multiple lines.

Code: ( Demo )

 $wedTrackTimes=[ "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" ]; foreach($wedTrackTimes as $time){ // iterate the time strings $timestamps[]=strtotime(strstr($time,'-',true)); // store the first time of the time range as a unix timestamp } array_multisort($timestamps,$wedTrackTimes); // use $timestamps to sort $wedTrackTimes var_export($wedTrackTimes); 

Output:

 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', ) 
0
source share

All Articles