What is the Ds\Set reverse() function in PHP?

Overview

The Ds\Set::reverse() function is used to reverse the order of elements. The elements are reversed in place, which means it does not use any extra space to reverse the set.

Syntax

The Ds\Set::reverse() function can be declared as shown below:

void public Ds\Set::reverse ( void ) 

Note: The Ds\Set::reverse() function does not have any input parameters.

Return value

The Ds\Set::reverse() function reverses the order of the elements of a set in place and does not have a return value.

Example

Consider the code snippet below, which demonstrates the use of the Ds\Set::reverse() function.

<?php
$set1 = new \Ds\Set([0, 2, 4, 6, 8]);
echo "set1 before reverse: ";
var_dump($set1);
$set1->reverse();
echo "\nset1 after reverse: ";
var_dump($set1);
?>

Output

set1 before reverse: object(Ds\Set)#1 (5) {
  [0]=>
  int(0)
  [1]=>
  int(2)
  [2]=>
  int(4)
  [3]=>
  int(6)
  [4]=>
  int(8)
}

set1 after reverse: object(Ds\Set)#1 (5) {
  [0]=>
  int(8)
  [1]=>
  int(6)
  [2]=>
  int(4)
  [3]=>
  int(2)
  [4]=>
  int(0)
}

Explanation

We declare a set set1 in line 3. We use the Ds\Set::reverse() function in line 8 to reverse the order of the elements in set1.

Free Resources