How to sort an array of times chronologically?

前端 未结 8 920
梦毁少年i
梦毁少年i 2021-01-17 18:39

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 sorte

相关标签:
8条回答
  • 2021-01-17 19:24

    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);
    
    0 讨论(0)
  • 2021-01-17 19:33

    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);
    }
    
    0 讨论(0)
  • 2021-01-17 19:34

    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);
    
    0 讨论(0)
  • 2021-01-17 19:38

    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.

    0 讨论(0)
  • 2021-01-17 19:38

    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);
    
    0 讨论(0)
  • 2021-01-17 19:40

    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 讨论(0)
提交回复
热议问题