• PHP Video Tutorials

PHP - Ds Set::diff() Function



The PHP Ds\Set::diff() function creates and returns a new set containing all the values that are not present in another set.

If the values in the first set are also present in the second set, this function returns an empty() set. For example set1 = ([1, 2]), and set2 = ([1, 2, 3]), so the set1->diff(set2) will be empty (), because set1 values 1 and 2 are present in set2.

Syntax

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

public Ds\Set Ds\Set::diff( Ds\Set $set )

Parameters

Following is the parameter of this function −

  • set − Another set containing the values to exclude.

Return value

This function returns a new set containing all values that aren't in another set.

Example 1

The following program demonstrates the usage of the Ds\Set::diff() function −

<?php  
   $set1 = new \Ds\Set([1, 2, 3]);
   echo "The set1 elements are: \n";
   print_r($set1);   
   $set2 = new \Ds\Set([3, 4, 5]);
   echo "The set2 elements are: \n";
   print_r($set2);   
   echo("The difference of set1 and set2: \n");  
   print_r($set1->diff($set2)); 
?>

Output

The above program generates the following output −

The set1 elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The set2 elements are:
Ds\Set Object
(
    [0] => 3
    [1] => 4
    [2] => 5
)
The difference of set1 and set2:
Ds\Set Object
(
    [0] => 1
    [1] => 2
)

Example 2

If all the elements of the first set are present in the second set as well, this function will return an empty () set.

Following is another example of the PHP Ds\Set::diff() function. We use this function to retrieve a new set containing all the elements that are not in this set ([10, 20, 30, 40, 50]) −

<?php  
   $set1 = new \Ds\Set([10, 20, 30]);
   echo "The set1 elements are: \n";
   print_r($set1);   
   $set2 = new \Ds\Set([10, 20, 30, 40, 50]);
   echo "The set2 elements are: \n";
   print_r($set2);   
   echo("The difference of set1 and set2: \n");  
   print_r($set1->diff($set2)); 
?>

Output

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

The set1 elements are:
Ds\Set Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
The set2 elements are:
Ds\Set Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The difference of set1 and set2:
Ds\Set Object
(
)

Example 3

If all the elements of the first set are not present in the second set, the PHP Ds\Set::diff() function returns a new set containing all the values that are not present in another set −

<?php  
   $set1 = new \Ds\Set(["Tutorials", "Point", "India"]);
   echo "The set1 elements are: \n";
   print_r($set1);   
   $set2 = new \Ds\Set(["Tutorix", "India"]);
   echo "The set2 elements are: \n";
   print_r($set2);   
   echo("The difference of set1 and set2: \n");  
   print_r($set1->diff($set2)); 
?>

Output

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

The set1 elements are:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The set2 elements are:
Ds\Set Object
(
    [0] => Tutorix
    [1] => India
)
The difference of set1 and set2:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
)
php_function_reference.htm
Advertisements