How can the syntax for shifting arrays in PHP be effectively formulated for user-defined rotation?

To rotate an array in PHP based on user-defined rotation, we can use a combination of array_slice and array_merge functions. By slicing the array into two parts based on the rotation index and merging them in the desired order, we can achieve the rotation effect.

function rotateArray($arr, $rotation) {
    $sliceIndex = $rotation % count($arr);
    $leftSlice = array_slice($arr, $sliceIndex);
    $rightSlice = array_slice($arr, 0, $sliceIndex);
    return array_merge($leftSlice, $rightSlice);
}

// Example usage
$array = [1, 2, 3, 4, 5];
$rotation = 2;
$rotatedArray = rotateArray($array, $rotation);
print_r($rotatedArray);