Skip to content
Advertisement

Rotate an array with weekday names as keys to start with tomorrow

I have the following array:

[
     [
         'schedules' => [
             'monday' => 1,
             'tuesday' => 1,
             'wednesday' => 1,
             'thursday' => 1,
             'friday' => 1,
             'saturday' => 0,
             'sunday' => 1,
         ]
    ]
]

I’d like to rotate the elements of this array with the first key being the tomorrow. Let’s say today was Wednesday, I’d want my array to look like this:

[
     [
         'schedules' => [
             'thursday' => 1,
             'friday' => 1,
             'saturday' => 0,
             'sunday' => 1,
             'monday' => 1,
             'tuesday' => 1,
             'wednesday' => 1,
         ]
    ]
]

I already have the weekday available (e.g. a string ‘thursday’). It gets passed into the function that I’m working with.

Advertisement

Answer

If you convert the day to a number 0-6 you can array_shift and array_push that many times to move the previous days to the end of the array.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement