Alex Rivera | Logout

Rotate array elements to the left (move first element to last and re-index)

Asked 2011-04-08T23:17:47.297
27

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?

Edit
Report

1 Answer

33
  $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
)
answered 2011-04-08T23:20:46.660

Your Answer