• PHP Video Tutorials

PHP - Ds Set::reversed() Function



The PHP Ds\Set::reversed() function is used to create a copy of the original set with values in reversed order. This function returns the reversed copy of the current set without affecting the original set.

In a reversed set, the order of elements is inverted, the element that was initially at the end of the set is moved to the first position, and the element that was at the beginning is moved to the last position.

Syntax

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

public Ds\Set::reversed(): Ds\Set

Parameters

This function does not accept any parameter.

Return value

This function returns a reversed copy of a set.

Example 1

The following is the basic example of the PHP Ds\Set::reversed() function −

<?php  
   $set = new \Ds\Set([1, 2, 3, 4, 5]);
   echo "The original set elements: \n";
   print_r($set);
   echo("The reversed set is: \n");
   #using the reversed() function
   print_r($set->reversed());
?>

Output

The above program generates the following output −

The original set elements:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The reversed set is:
Ds\Set Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Example 2

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

<?php  
   $set = new \Ds\Set(["Tutorials", "Point", "India"]); 
   echo "The original set elements: \n"; 
   print_r($set); 
   echo("The reversed set elements: \n");
   #using the reversed() function
   print_r($set->reversed());  
?> 

Output

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

The original set elements:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The reversed set elements:
Ds\Set Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)

Example 3

In the example below, we use the reversed() function to retrieve the reversed copy of the current set (['a', 'e', 'i', 'o', 'u']) −

<?php  
   $set = new \Ds\Set(['a', 'e', 'i', 'o', 'u']); 
   echo "The original set elements: \n"; 
   print_r($set); 
   echo("The reversed set elements: \n");
   #using the reversed() function
   print_r($set->reversed());  
?>

Output

Once the above program is executed, it will display the following output −

The original set elements:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The reversed set elements:
Ds\Set Object
(
    [0] => u
    [1] => o
    [2] => i
    [3] => e
    [4] => a
)
php_function_reference.htm
Advertisements