Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP按值对对象数组进行排序_Php_Arrays_Sorting - Fatal编程技术网

PHP按值对对象数组进行排序

PHP按值对对象数组进行排序,php,arrays,sorting,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_t

我有一个周末数组,如:-

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)
    );
我想按天对该数组进行排序,如下所示: 假设今天是星期四,那么星期四的钥匙就在前面。接下来是星期五、星期六、星期天、星期一、星期二、星期三。例如:-

如果今天是星期四,则数组的排序如下:-

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

如何使用对象数组进行这种类型的排序?

我认为应该这样做:

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

为了达到预期的效果,您可以使用以下功能:

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>";
echo”“;
印刷(轮换(星期四);
回声“;


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

这将使

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
注意:它将保留关键点,如果希望关键点按从零开始的升序排列,请替换为

解释:为什么这件事有效

下面是
日期('d-m-Y',strottime(day))的输出截至2015年6月18日(星期四)

观察上述情况,并根据给定的日期生成即将到来的日期

这意味着昨天是2015年6月17日的
周三
,但是当
周三
被赋予一个
日期
时,它会生成即将到来的
周三
日期

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

string'22-06-2015'//星期一
字符串'23-06-2015'//星期二
字符串“24-06-2015”//星期三

字符串'25-06-2015'//星期四引用数组旋转从哪里获得此信息?这看起来不像数组,但确实像是从某种APIs获取的对象。它是一个对象数组。它是从服务器上的var_dump复制的,所以它看起来像这样@SiimKallari@GiteshPurbia它是否总是按周中的升序排列?是的,顺序是相同的。但它将根据当前日期进行更改@扎祖
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
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