Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Vector::reversed() Function



The PHP Ds\Vector::reversed() function is used to create a copy of the original vector with values in reversed order and returns the reversed copy of this vector.

In a reversed vector, the order of elements is inverted, the element that was initially at the end of the vector is moved to the first position, and the element that was at the beginning is moved to the last position.

Syntax

Following is the syntax of the PHP Ds\Vector::reversed() function −

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

Parameters

This function does not accept any parameter.

Return value

This function returns a reversed copy of the vector.

Example 1

The following program demonstrates the usage of the PHP Ds\Vector::reversed() function −

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The original vector: \n";   
   print_r($vector);
   echo("The copy of the reversed vector: \n");
   #using reversed() function
   print_r($vector->reversed()); 
?>

Output

The above program produces the following output −

The original vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The copy of the reversed vector:
Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Example 2

Following is another example of the PHP Ds\Vector::reversed() function. We use this function to retrieve the reversed copy of this vector (["Tutorials", "Point", "Tutorix"]) −

<?php 
   $vector= new \Ds\Vector(["Tutorials", "Point", "Tutorix"]);
   echo "The original vector: \n";
   print_r($vector);  
   echo("The reversed of a vector is: \n");
   #using reversed() function
   print_r($vector->reversed()); 
?>

Output

Once the above program is executed, the following output will be displayed −

The original vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
The reversed of a vector is:
Ds\Vector Object
(
    [0] => Tutorix
    [1] => Point
    [2] => Tutorials
)
php_function_reference.htm
Advertisements