Wednesday, November 20, 2024
Google search engine
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

Most Popular

Recent Comments