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

PHP - Ds Sequence::reversed() Function



The PHP Ds\Sequence::reversed() function is used to retrieve a reversed copy of a current sequence. The reversed copy is a duplicate copy of the original sequence where the elements are arranged in reverse order.

This function does not affect the current instance instead, it returns a new sequence.

Syntax

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

abstract public Ds\Sequence::reversed(): Ds\Sequence

Parameters

This function does not accept any parameter.

Return value

This function returns a reversed copy of the sequence.

Example 1

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

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

Output

The above program produces the following output −

The original sequence:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
After reversing the sequence:
Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Example 2

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

<?php  
   $seq = new \Ds\Vector(["Tutorials", "Point", "India"]);
   echo("The original sequence: \n"); 
   print_r($seq);
   echo("The reversed copy of the sequence: \n");
   #using reversed() function
   print_r($seq->reversed());  
?>

Output

After executing the above program, the following output will be displayed −

The original sequence:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The reversed copy of the sequence:
Ds\Vector Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)
php_function_reference.htm
Advertisements