PHP按值排序周末对象数组

时间:2015-06-18 10:17:31

标签: php arrays sorting

我有一个周末阵列,如: -

array (
      0 => 
        object(stdClass)[72]
          public 'id' => string '103' 
          public 'day' => string 'monday' (length=6)
          public 'time_from' => string '12:30am' (length=7)
          public 'time_to' => string '12:30am' (length=7)
      1 => 
        object(stdClass)[71]
          public 'id' => string '104' (length=3)
          public 'day' => string 'tuesday' (length=7)
          public 'time_from' => string '12:00am' (length=7)
          public 'time_to' => string '1:00am' (length=6)
      2 => 
        object(stdClass)[70]
          public 'id' => string '105' (length=3)
          public 'day' => string 'wednesday' (length=9)
          public 'time_from' => string '12:00pm' (length=7)
          public 'time_to' => string '12:30pm' (length=7)
      3 => 
        object(stdClass)[69]
          public 'id' => string '106' (length=3)
          public 'day' => string 'thursday' (length=8)
          public 'time_from' => string '2:00pm' (length=6)
          public 'time_to' => string '7:00pm' (length=6)
      4 => 
        object(stdClass)[68]
          public 'id' => string '107' (length=3)
          public 'day' => string 'friday' (length=6)
          public 'time_from' => string '12:00am' (length=7)
          public 'time_to' => string '12:30am' (length=7)
      5 => 
        object(stdClass)[67]
          public 'id' => string '108' (length=3)
          public 'day' => string 'saturday' (length=8)
          public 'time_from' => string '12:00am' (length=7)
          public 'time_to' => string '12:30am' (length=7)
      6 => 
        object(stdClass)[67]
          public 'id' => string '108' (length=3)
          public 'day' => string 'sunday' (length=8)
          public 'time_from' => string '12:00am' (length=7)
          public 'time_to' => string '12:30am' (length=7)
    );

我希望按照一种方式对这个数组进行排序: 假设今天是星期四那么星期四的关键如果先来。接下来是Firday,Saterday,周日,周一,周二,周三。例如: -

如果今天是星期四那么阵列将类似于: -

array (
          3 => 
            object(stdClass)[69]
              public 'id' => string '106' (length=3)
              public 'day' => string 'thursday' (length=8)
              public 'time_from' => string '2:00pm' (length=6)
              public 'time_to' => string '7:00pm' (length=6)
          4 => 
            object(stdClass)[68]
              public 'id' => string '107' (length=3)
              public 'day' => string 'friday' (length=6)
              public 'time_from' => string '12:00am' (length=7)
              public 'time_to' => string '12:30am' (length=7)
          5 => 
            object(stdClass)[67]
              public 'id' => string '108' (length=3)
              public 'day' => string 'saturday' (length=8)
              public 'time_from' => string '12:00am' (length=7)
              public 'time_to' => string '12:30am' (length=7)
          6 => 
            object(stdClass)[67]
              public 'id' => string '108' (length=3)
              public 'day' => string 'sunday' (length=8)
              public 'time_from' => string '12:00am' (length=7)
              public 'time_to' => string '12:30am' (length=7)
          0 => 
            object(stdClass)[72]
              public 'id' => string '103' 
              public 'day' => string 'monday' (length=6)
              public 'time_from' => string '12:30am' (length=7)
              public 'time_to' => string '12:30am' (length=7)
          1 => 
            object(stdClass)[71]
              public 'id' => string '104' (length=3)
              public 'day' => string 'tuesday' (length=7)
              public 'time_from' => string '12:00am' (length=7)
              public 'time_to' => string '1:00am' (length=6)
          2 => 
            object(stdClass)[70]
              public 'id' => string '105' (length=3)
              public 'day' => string 'wednesday' (length=9)
              public 'time_from' => string '12:00pm' (length=7)
              public 'time_to' => string '12:30pm' (length=7)
        );

如何使用对象数组进行此类排序?

3 个答案:

答案 0 :(得分:0)

我认为这应该有效:

function byDaySort($array)
{
    $dayNo = date('N') - 1;
    if($dayNo == 0)
        return $array;
    elseif($dayNo == 6)
        return array_reverse($array);
    else
        return array_merge(array_slice($array, $dayNo), array_slice($array, 0, 6 - $dayNo));
}

答案 1 :(得分:0)

要获得所需的结果,您可以使用以下功能:

function rotateArray($arr, $firstDay) {
    $firstElement = reset($arr);
    while($firstElement->day != $firstDay) {
        array_push($arr, array_shift($arr));
        $firstElement = reset($arr);
    }
    return $arr;
}

您可以使用它:

echo "<pre>";
print_r(rotateArray($arr, 'thursday'));
echo "</pre>";

<小时/> 免责声明:您还应该检查$firstDay数组中是否存在$arr的值,以避免无限循环:)

答案 2 :(得分:0)

这将使其成为可能,

uasort($your_array, function ($a, $b){
    return strtotime($a->day) - strtotime($b->day);
});

注意:它会保留密钥,如果您希望按升序从0开始,请将uasort替换为usort

解释:为什么这个有用?

以下是date('d-m-Y',strtotime(day));的输出18-06-2015 (thursday)

string '22-06-2015' // monday
string '23-06-2015' // tuesday
string '24-06-2015' // wednesday
string '18-06-2015' // thursday
string '19-06-2015' // friday
string '20-06-2015' // satuday
string '21-06-2015' // sunday

观察上述date()strtotime()将根据给定的日期生成即将到来的日期。

这意味着昨天在wednesday17-06-2015,但是当wednesday提出date时,它会生成即将到来的wednesday's日期。

如果明天在19-06-2015上转储相同内容,您将获得如下输出

string '22-06-2015' // monday
string '23-06-2015' // tuesday
string '24-06-2015' // wednesday
string '25-06-2015' // thursday   <-- This date changed
string '19-06-2015' // friday
string '20-06-2015' // satuday
string '21-06-2015' // sunday

所以,基本上,这就是我们想要的。

将其与uasort()一起使用可以让它按照我们的意愿运作。