PHP Ds\Set reverse() Function

The Ds\Set::reverse() function of Ds\Set class in PHP is an inbuilt function which is used to reverse the order of elements present in the Set instance. This function reverses the Set in-place. That is, it does not uses any extra space and updates the original Set instance with reversed values.

Syntax:

void public Ds\Set::reverse ( void ) 

Parameters: This function does not accepts any parameters.



Return Value: This function does not returns any value. It reverses the order of elements in the actual Set instance.

Below programs illustrate the Ds\Set::reverse() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Declare new Set
$set = new \Ds\Set(["10", "20", "30"]); 
  
// Display the initial Set element 
echo "Initial Set: \n";
var_dump($set); 
  
// Reverse the set
$set->reverse();
  
// Print the reversed Set
echo "\nReversed Set is: \n";
var_dump($set);
  
?> 

chevron_right


Output:

Initial Set: 
object(Ds\Set)#1 (3) {
  [0]=>
  string(2) "10"
  [1]=>
  string(2) "20"
  [2]=>
  string(2) "30"
}

Reversed Set is: 
object(Ds\Set)#1 (3) {
  [0]=>
  string(2) "30"
  [1]=>
  string(2) "20"
  [2]=>
  string(2) "10"
}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Declare new Set
$set = new \Ds\Set(["Geeks", "for", "Keegs"]); 
  
// Display the initial Set element 
echo "Initial Set: \n";
var_dump($set); 
  
// Reverse the set
$set->reverse();
  
// Print the reversed Set
echo "\nReversed Set is: \n";
var_dump($set);
  
?> 

chevron_right


Output:

Initial Set: 
object(Ds\Set)#1 (3) {
  [0]=>
  string(5) "Geeks"
  [1]=>
  string(3) "for"
  [2]=>
  string(5) "Keegs"
}

Reversed Set is: 
object(Ds\Set)#1 (3) {
  [0]=>
  string(5) "Keegs"
  [1]=>
  string(3) "for"
  [2]=>
  string(5) "Geeks"
}

Reference: http://php.net/manual/en/ds-set.reverse.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.