PHP: 'rotate' an array?

Dylan picture Dylan · Apr 9, 2011 · Viewed 22.3k times · Source

is it possible to easily 'rotate' an array in PHP ?

Like this: 1, 2, 3, 4 -> 2, 3 ,4 ,1

Is there some kind of built-in PHP function for this?

Answer

Wh1T3h4Ck5 picture Wh1T3h4Ck5 · Apr 9, 2011
  $numbers = array(1,2,3,4);
  array_push($numbers, array_shift($numbers));
  print_r($numbers);

Output

Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 1
)