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

时间:2022-10-31 14:05:00

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.

但它不是返回一个有序数组,而是返回1.我假设它是因为它是非关联的,所以没有键。有没有办法按值对数组进行排序?我们确实需要在上午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?

这可能吗?

8 个解决方案

#1


5  

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

#2


21  

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:

按引用排序(这意味着它会对传递给它的任何内容进行排序),它会根据失败返回true / false。你在这做什么:

$wedTrackTimes = sort($wedTrackTimes);

is assigning the value $wedTrackTimes to TRUE or FALSE.

将$ wedTrackTimes赋值为TRUE或FALSE。

Try

尝试

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

#3


5  

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

没错,sort返回bool。只需使用:

sort($wedTrackTimes);

#4


5  

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.

像所有php的排序函数一样排序,就地排序。如果排序成功则返回true,否则返回false。如果您只对字符串/数字进行排序,则此结果无关紧要。

$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.

是要走的路。

#5


2  

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,表示是否可以排序或发生错误。

#6


2  

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

#7


0  

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

#8


0  

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

按时间顺序排序(按时间顺序排列)会调用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()和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',
)

#1


5  

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

#2


21  

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:

按引用排序(这意味着它会对传递给它的任何内容进行排序),它会根据失败返回true / false。你在这做什么:

$wedTrackTimes = sort($wedTrackTimes);

is assigning the value $wedTrackTimes to TRUE or FALSE.

将$ wedTrackTimes赋值为TRUE或FALSE。

Try

尝试

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

#3


5  

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

没错,sort返回bool。只需使用:

sort($wedTrackTimes);

#4


5  

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.

像所有php的排序函数一样排序,就地排序。如果排序成功则返回true,否则返回false。如果您只对字符串/数字进行排序,则此结果无关紧要。

$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.

是要走的路。

#5


2  

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,表示是否可以排序或发生错误。

#6


2  

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

#7


0  

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

#8


0  

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

按时间顺序排序(按时间顺序排列)会调用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()和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',
)