Open In App

PHP | Ds\Vector reversed() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The Ds\Vector::reversed() function is an inbuilt function in PHP which is used to reverse the elements of vector after copying the elements of original vector into a copy.

Syntax:

public Ds\Vector::reversed( void ) : Ds\Vector

Parameters: This function does not accept any parameter.

Return Value: This function returns a copy of the original vector in reverse order. Also, the original vector will have no effect.

Below is the program to illustrate the Ds\Vector::reversed() function in PHP:

Program 1:




<?php
  
// Create new Vector
$arr = new \Ds\Vector([1, 2, 3, 4, 5]);
  
// Display the elements
var_dump($arr);
  
echo("Vector after reversing\n");
  
// Use reversed() function to reverse 
// the copy of vector and display it
var_dump($arr->reversed());
  
?>


Output:

object(Ds\Vector)#1 (5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
}
Vector after reversing
object(Ds\Vector)#2 (5) {
  [0]=>
  int(5)
  [1]=>
  int(4)
  [2]=>
  int(3)
  [3]=>
  int(2)
  [4]=>
  int(1)
}

Program 2:




<?php
  
// Create new Vector
$arr = new \Ds\Vector(["Learn", "data", "structures"]);
  
// Display the elements
print_r($arr);
  
echo("Vector after reversing\n");
  
// Use reversed() function to reverse 
// the copy of vector and display it
print_r($arr->reversed());
  
?>


Output:

Ds\Vector Object
(
    [0] => Learn
    [1] => data
    [2] => structures
)
Vector after reversing
Ds\Vector Object
(
    [0] => structures
    [1] => data
    [2] => Learn
)

Reference: http://php.net/manual/en/ds-vector.reversed.php



Last Updated : 29 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads