-1

So here's my array:

array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

How could I change the starting day to say:

array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

This is probably ridiculously easy but my brain hurts from trying to figure this out.

David
  • 17
  • 6

1 Answers1

1

You can use the next code snippet (two lines in fact):

$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

// Remove the last element "Sunday" from the array
$lastDay = array_pop($days); 
  
// Append the last day at the beginning of the array
$days = [$lastDay, ...$days];

print_r($days);

Demo

Aksen P
  • 4,564
  • 3
  • 14
  • 27
  • 1
    Can be made into one line `$days = [ array_pop($days), ...$days ]` . – apokryfos Apr 09 '23 at 07:21
  • 1
    @apokryfos, yeap, it's obvious. But for debugging these two lines are better. – Aksen P Apr 09 '23 at 07:29
  • This is helpful but what if I need to start with Thursday? I take it there's no function to shift the array? Will I need to just write a function to rework the array based on which day I need to show first? – David Apr 11 '23 at 23:32
  • @David, in-built function - no, but a custom function could be created. – Aksen P Apr 12 '23 at 04:03