• PHP Video Tutorials

PHP - Ds Set::union() Function



The PHP Ds\Set::union() function is used to create a new set by combining the values from the current instance with those from another set. The term "union" refers to both the common and non-common values present in both sets.

The below expression clearly explains the workings of this function −

𝐴 ∪ 𝐵 = { 𝑥 : 𝑥 ∈ 𝐴 ∨ 𝑥 ∈ 𝐵 }

Syntax

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

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

Parameters

This function accepts a single parameter as 'set', which is described below −

  • set − This parameter specifies the other set.

Return value

This function returns a new set containing all values of the current instance and another set.

Example 1

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

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

Output

The above program produces the following output −

The set1 elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The set2 elements are:
Ds\Set Object
(
    [0] => 4
    [1] => 5
    [2] => 6
)
The union of both set:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Example 2

The following is another example of the PHP Ds\Set::union() function. We use this function to create a new set using values from the current instance ([2, 3, 6, 7, 8]) and another set ([2, 3, 5, 8, 9, 10]).

<?php  
   $set1 = new \Ds\Set([2, 3, 6, 7, 8]);
   echo "The set1 elements are: \n";
   print_r($set1);
   $set2 = new \Ds\Set([2, 3, 5, 8, 9, 10]);
   echo "The set2 elements are: \n";   
   print_r($set2);
   echo("The union of both set: \n");  
   var_dump($set1->union($set2)); 
?>

Output

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

The set1 elements are:
Ds\Set Object
(
    [0] => 2
    [1] => 3
    [2] => 6
    [3] => 7
    [4] => 8
)
The set2 elements are:
Ds\Set Object
(
    [0] => 2
    [1] => 3
    [2] => 5
    [3] => 8
    [4] => 9
    [5] => 10
)
The union of both set:
object(Ds\Set)#3 (8) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(6)
  [3]=>
  int(7)
  [4]=>
  int(8)
  [5]=>
  int(5)
  [6]=>
  int(9)
  [7]=>
  int(10)
}

Example 3

If both the sets contain similar elements, the union() function creates a new set containing the elements from both set's common and non-common values.

<?php 
   $set1 = new \Ds\Set(['a', 'e', 'i']);  
   $set2 = new \Ds\Set(['a', 'e', 'i']);  
   echo "The set1 elements are: \n";
   print_r($set1);
   echo "The set2 elements are: \n";
   print_r($set2);
   echo "The union of both set: \n";  
   print_r($set1->union($set2)); 
?>

Output

The set1 elements are:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
)
The set2 elements are:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
)
The union of both set:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
)

php_function_reference.htm
Advertisements