PHP | Ds\Vector rotate() Function

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 happens in-place.

Syntax:

public Ds\Vector::rotate( $rotations ) : void

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new Vector
$vect = new \Ds\Vector([1, 2, 3, 4, 5]);
  
echo("Original Vector\n");
  
// Display the Vector elements
print_r($vect);
  
// Use rotate() function to rotate
// the vector elements
$vect->rotate(3);
  
echo("\nVector after rotating by 3 places\n");
  
// Display the Vector elements
print_r($vect);
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new Vector
$vect = new \Ds\Vector([1, 2, 3, 4, 5]);
  
echo("Original Vector\n");
  
// Display the Vector elements
print_r($vect);
  
// Use rotate() function to rotate
// the vector elements
$vect->rotate(6);
  
echo("\nVector after rotating by 6 places\n");
  
// Display the Vector elements
print_r($vect);
  
?>

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.