开发者

How to sort an array of times chronologically?

开发者 https://www.devze.com 2023-03-15 04:18 出处:网络
I have a non-associative array where the data that comes in is not sorted (I\'m receiving the data from an outside system and cannot force it to come into the array in sorted order.) Is there any way

I have a non-associative array where the data that comes in is not sorted (I'm receiving the data from an outside system and cannot force it to come into the array in sorted order.) Is there any way to sort the values? I've 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'm assuming it's because it's non-associative, so there are no keys. Is there any way to sort an array by value only? We really need the 9:30 AM time slot to fall after the 8:15 AM slot, as it should.

UPDATE

Thanks to all for the answers; that did make the array sort, but not as expected. If I use the default sort type, I get this:

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 this:

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
)

Using SORT_STRING I get this:

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 A开发者_Go百科M-9:15 AM
    [4] => 9:30 AM-10:30 AM
)

What I need is:

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 this possible?


Sort works by reference (that means it sorts whatever you pass to it), it returns true/false based on failure. What you're doing here:

$wedTrackTimes = sort($wedTrackTimes);

is assigning the value $wedTrackTimes to TRUE or FALSE.

Try

sort($wedTrackTimes);
print_r($wedTrackTimes);


So, it looks like you're looking for something a little more advanced than a standard sort.

// 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);
}


That's right, sort returns bool. Just use that:

sort($wedTrackTimes);


sort, like all of php's sorting functions, sorts in-place. It returns true if the sorting was successful, false otherwise. This result is irrelevant if you're only sorting 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);

is the way to go.


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 if sorting was possible or an error occured.


Remove $wedTrackTimes = before sort.

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


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

Don't assign variable to sort function, Basically what you are doing is assigning sort's value (which will be true or false) into wedTrackTimes, Instead of this use

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


Sensibly sorting on time (chronologically) will call for strtotime().

Here is a one-liner for you using array_multisort(). array_map() and strtotime() are called to generate an array to be used purely for the sorting order.

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

For anyone that is lost in that syntax, here is the same functionality on more 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

精彩评论

暂无评论...
验证码 换一张
取 消