Skip to content
Advertisement

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

I have the following array:

JavaScript

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:

JavaScript

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