0

I have an weekend array like :-

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

I want to sort this array by a day like a way: suppose today is thursday then the key of thursday if comes first. and then next come Firday,Saterday,Sunday,Monday,Tuesday, Wednesday. for ex:-

IF today is Thursday then array will sort like:-

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

How can i do this type of sorting with array of objects ?

viral
  • 3,724
  • 1
  • 18
  • 32
Gitesh Purbia
  • 1,094
  • 1
  • 12
  • 26

3 Answers3

0

I think this should work:

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));
}
iamawebgeek
  • 2,713
  • 1
  • 18
  • 34
0

To achieve the desired results, you may use the following function:

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

You may use it like:

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


Disclaimer: You should also check for the existence of the value of the $firstDay in the array of $arr to avoid an infinite loop :)
someOne
  • 1,975
  • 2
  • 14
  • 20
0

This will make it possible,

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

Note: It will preserve keys, if you want keys in ascending order starting with zero, replace uasort with usort.

Explanation: Why this thing works?

Below is output of date('d-m-Y',strtotime(day)); as on 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

Observing the above date() and strtotime() will produce the upcoming dates based on the day given.

That means yesterday was wednesday on 17-06-2015, but when wednesday is given to make a date, it produces the upcoming wednesday's date.

If you dump the same tomorrow on 19-06-2015, you will get output like below,

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

So, basically, that's what we want.

Using this with uasort() makes it work as we want.

viral
  • 3,724
  • 1
  • 18
  • 32