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

时间:2011-06-22 16:46:51

标签: php arrays sorting time

我有一个非关联数组,其中输入的数据没有排序(我从外部系统接收数据,并且不能强制它按排序顺序进入数组。)有没有办法排序价值?我试过这个:

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

但是它没有返回一个有序数组,而是返回1.我假设它是因为它是非关联的,所以没有键。有没有办法按值排序数组?我们确实需要在上午8点15分之前的时间段上午9点30分,这应该是。

更新

感谢大家的答案;确实使数组排序,但不如预期。如果我使用默认排序类型,我会得到:

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
)

使用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
)

使用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
)

我需要的是:

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


)

这可能吗?

8 个答案:

答案 0 :(得分:21)

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

$wedTrackTimes = sort($wedTrackTimes);

将$ wedTrackTimes赋值为TRUE或FALSE。

尝试

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

答案 1 :(得分:5)

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

sort($wedTrackTimes);

答案 2 :(得分:5)

sort,就像所有php的sorting functions一样,就地排序。如果排序成功则返回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);

是要走的路。

答案 3 :(得分:5)

所以,看起来你正在寻找比标准排序更先进的东西。

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

答案 4 :(得分:2)

将您的代码更改为:

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

您可以在the documentation中阅读,sort()的返回值为true / 1或false / 0,表示是否可以排序或发生错误。

答案 5 :(得分:2)

排序前删除$ 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);

答案 6 :(得分:0)

在您的代码中

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

不要将变量赋给sort函数, 基本上你正在做的是将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)

按时间顺序排序(按时间顺序排列)会调用strtotime()

以下是使用array_multisort()的单行内容。调用array_map()strtotime()来生成一个纯粹用于排序顺序的数组。

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

对于那些在语法中丢失的人来说,这里有更多行的相同功能。

代码:(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);

输出:

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