Sunday, August 2, 2026
HomeLanguagesPHP | DsVector rotate() Function

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 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
  
// 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);
  
?>


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
  
// 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);
  
?>


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

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS