30 seconds of PHP

  Back to Home

rotate

arraybeginner

Rotates the array (in left direction) by the number of shifts.

Given the $shift index, merge the array values after $shift with the values before $shift.

function rotate($array, $shift = 1)
{
  for ($i = 0; $i < $shift; $i++) {
    array_push($array, array_shift($array));
  }

  return $array;
}