The Ds\Vector::rotate() function is an inbuilt function in PHP which is used to rotate the array elements by a given number of rotation. Rotations also happen in-place.
Syntax:
void public Ds\Vector::rotate( $rotations )
Parameters: This function accepts single parameter $rotations which holds the number of rotations.
Return Value: This function does not return any value.
Below programs illustrate the Ds\Vector::rotate() function in PHP:
Program 1:
<?php
$vect = new \Ds\Vector([1, 2, 3, 4, 5]);
echo ( "Original Vector\n" );
print_r( $vect );
$vect ->rotate(3);
echo ( "\nVector after rotating by 3 places\n" );
print_r( $vect );
?>
|
Output:
Original Vector
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Vector after rotating by 3 places
Ds\Vector Object
(
[0] => 4
[1] => 5
[2] => 1
[3] => 2
[4] => 3
)
Program 2: When number of rotations are greater than the number of elements in the vector.
<?php
$vect = new \Ds\Vector([1, 2, 3, 4, 5]);
echo ( "Original Vector\n" );
print_r( $vect );
$vect ->rotate(6);
echo ( "\nVector after rotating by 6 places\n" );
print_r( $vect );
?>
|
Output:
Original Vector
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Vector after rotating by 6 places
Ds\Vector Object
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 1
)
Reference: http://php.net/manual/en/ds-vector.rotate.php