简体   繁体   English

如何按时间顺序对数组进行排序?

[英]How to sort an array of times chronologically?

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. 但它没有返回一个排序的数组,而是返回1.我假设它是因为它是非关联的,所以没有键。 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. 我们确实需要在上午8:15时段之后的上午9:30时段落下,应该如此。

UPDATE 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: 使用SORT_NUMERIC我得到这个:

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: 使用SORT_STRING我得到这个:

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 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. 按引用排序(这意味着它会对传递给它的任何内容进行排序),它会根据失败返回true / false。 What you're doing here: 你在这做什么:

$wedTrackTimes = sort($wedTrackTimes);

is assigning the value $wedTrackTimes to TRUE or FALSE. 将值$ wedTrackTimes赋值为TRUE或FALSE。

Try 尝试

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

That's right, sort returns bool . 没错,sort返回bool Just use that: 只需使用:

sort($wedTrackTimes);

sort , like all of php's sorting functions , sorts in-place. sort ,像所有的PHP的排序功能 ,分类原地。 It returns true if the sorting was successful, false otherwise. 如果排序成功则返回true,否则返回false。 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. 是要走的路。

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

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. 正如您在文档中看到的那样, sort()的返回值为true / 1或false / 0,表示是否可以进行排序或发生错误。

Remove $wedTrackTimes = before sort. 排序前删除$ wedTrackTimes =

 $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 不要将变量赋值给sort函数,基本上你正在做的是将sort的值(将为true或false)赋值给wedTrackTimes,而不是这个用法

$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() . 按时间顺序排序(按时间顺序排列)会调用strtotime()

Here is a one-liner for you using array_multisort() . 这是使用array_multisort() array_map() and strtotime() are called to generate an array to be used purely for the sorting order. array_map()strtotime()来生成一个纯粹用于排序顺序的数组。

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',
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM