PHP - Ds Sequence::reverse() Function
The PHP Ds\Sequence::reverse() function is used to reverse a sequence in-place. The term 'in-place' refers that the reversal is done without creating or allocating memory for a new sequence, instead, this function modifies the existing sequence directly.
Syntax
Following is the syntax of the PHP Ds\Sequence::reverse() function −
public abstract void Ds\Sequence::reverse( void )
Parameters
This function does not accept any parameter.
Return value
This function does not return any value.
Example 1
The following is the basic example of the PHP Ds\Sequence::reverse() function −
<?php
$seq = new \Ds\Vector([1, 2, 3, 4, 5]);
echo("The original sequence before reversing: \n");
print_r($seq);
echo("The Sequence after reversing: \n");
#using reverse() function
$seq->reverse();
print_r($seq);
?>
Output
The above program produces the following output −
The original sequence before reversing:
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
The Sequence after reversing:
Ds\Vector Object
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
Example 2
Following is another example of the PHP Ds\Sequence::reverse() function. We use this function to reverse the order of this sequence (["Tutorials", "Point", "India"]) elements −
<?php
$seq = new \Ds\Vector(["Tutorials", "Point", "India"]);
echo("The original Sequence before reversing: \n");
print_r($seq);
echo("The Sequence after reversing: \n");
#using reverse() function
$seq->reverse();
print_r($seq);
?>
Output
After executing the above program, the following output will be displayed −
The original Sequence before reversing:
Ds\Vector Object
(
[0] => Tutorials
[1] => Point
[2] => India
)
The Sequence after reversing:
Ds\Vector Object
(
[0] => India
[1] => Point
[2] => Tutorials
)