• PHP Video Tutorials

PHP - Ds Set remove() Function



The PHP Ds\Set::remove() function is used to remove all the specified elements from the current set; if found. If the specified values are not present in this set, no values will be removed, and the set will remain unchanged.

For example, if you pass three values 2, 3, and 5 to remove from set (1, 2, 3). Only the 2, and 3 will be removed from this set.

Syntax

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

public void Ds\Set::remove([ mixed $...values ])

Parameters

Following is the parameter of this function −

  • values −  The one or more values need to be removed.

Return value

This function does not return any value.

Example 1

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

<?php  
   $set = new \Ds\Set([1, 2, 3, 4, 5]); 
   echo "The original set is: \n"; 
   print_r($set); 
   $val1 = 2;
   $val2 = 3;
   echo "Values need to be removed:" . $val1.",". $val2;
   $set->remove($val1, $val2); 
   echo "\nThe set after removing values: \n"; 
   print_r($set); 
?>

Output

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

The original set is:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Values need to be removed:2,3
The set after removing values:
Ds\Set Object
(
    [0] => 1
    [1] => 4
    [2] => 5
)

Example 2

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

<?php  
   $set = new \Ds\Set(["Tutorials", "Point", "India"]); 
   echo "The original set is: \n"; 
   print_r($set); 
   $val = "Point";
   echo "The value to remove:". $val;
   $set->remove($val); 
   echo "\nThe set after removing values: \n"; 
   print_r($set); 
?>

Output

The above program generates the following output −

The original set is:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The value to remove:Point
The set after removing values:
Ds\Set Object
(
    [0] => Tutorials
    [1] => India
)

Example 3

If the specified value is not found in the current set, this function does not remove any value and the set will remain unchanged.

<?php  
   $set = new \Ds\Set(['a', 'e', 'i', 'o', 'u']); 
   echo "The original set is: \n"; 
   print_r($set); 
   $val = 'A';
   echo "The value to remove:". $val;
   $set->remove($val); 
   echo "\nThe set after removing values: \n"; 
   print_r($set); 
?>

Output

On executing the above program, it will generate the following output −

The original set is:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The value to remove:A
The set after removing values:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
php_function_reference.htm
Advertisements