• PHP Video Tutorials

PHP - Ds Set::reverse() Function



The PHP Ds\Set::reverse() function reverses the order of the current set. This function reverses the set in-place.

This means that it does not create a new set or require any additional space. It simply updates the original set values by reversing their order.

Syntax

Following is the syntax of the PHP Ds\Set::reverse() function −

public void Ds\Set::reverse( void )

Parameters

This function does not accept any parameter.

Return value

This function does not return any value, instead, reverses the current set.

Example 1

The following program demonstrates the usage of the PHP Ds\Set::reverse() function.

<?php  
   $set = new \Ds\Set([10, 20, 30]);  
   echo "The original set (before reverse): \n"; 
   var_dump($set);  
   $set->reverse(); 
   echo "\nThe set (after reverse): \n"; 
   var_dump($set); 
?> 

Output

The above program produces the following output −

The original set (before reverse):
object(Ds\Set)#1 (3) {
  [0]=>
  int(10)
  [1]=>
  int(20)
  [2]=>
  int(30)
}

The set (after reverse):
object(Ds\Set)#1 (3) {
  [0]=>
  int(30)
  [1]=>
  int(20)
  [2]=>
  int(10)
}

Example 2

The following is another example of the PHP Ds\Set::reverse() function. We use this function to reverse the values of this set (["Tutorials", "Point", "India"]).

<?php  
   $set = new \Ds\Set(["Tutorials", "Point", "India"]);  
   echo "The original set(before reverse): \n"; 
   var_dump($set);
   $set->reverse(); 
   echo "\nThe set after reversed: \n"; 
   var_dump($set);
?>

Output

After executing the above program, it will display the following output −

The original set(before reverse):
object(Ds\Set)#1 (3) {
  [0]=>
  string(9) "Tutorials"
  [1]=>
  string(5) "Point"
  [2]=>
  string(5) "India"
}

The set after reversed:
object(Ds\Set)#1 (3) {
  [0]=>
  string(5) "India"
  [1]=>
  string(5) "Point"
  [2]=>
  string(9) "Tutorials"
}
php_function_reference.htm
Advertisements