• PHP Video Tutorials

PHP - Ds Set::intersect() Function



The PHP Ds\Set::intersect() function is used to create a new set by intersecting the values with another set. Intersection refers to the common values in both sets.

This function uses values common to both the current instance and another set. That is, a copy of the current instance is returned with all values removed that are not present in the other set.

The below expression clearly explains the workings of this function −

A ∩ B = {x : x ∈ A ∧ x ∈ B}

Syntax

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

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

Parameters

Following is the parameter of this function −

  • set − This parameter specifies the other set.

Return value

This function returns the intersection of the current instance and another set.

Example 1

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

<?php
$set1 = new \Ds\Set([1,2,3,4,5,6,7]);
echo "Set1 elements are: \n";
print_r($set1);
$set2 = new \DS\Set([1,2,3,4]);
echo "Set2 elements are: \n";
print_r($set2);
echo "The elements after the intersection: \n";
var_dump($set1->intersect($set2)); 
?>

Output

The above program produces the following output −

Set1 elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
)
Set2 elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
The elements after the intersection:
object(Ds\Set)#3 (4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
}

Example 2

The following is another example of the PHP Ds\Set::intersect() function. We use this function to create a new set that intersects the values with another set ([84, 85, 86]) −

<?php
$set1 = new \Ds\Set([81, 82, 83]);
echo "Set1 elements are: \n";
print_r($set1);
$set2 = new \DS\Set([84, 85, 86]);
echo "Set2 elements are: \n";
print_r($set2);
echo "The elements after the intersection: \n";
var_dump($set1->intersect($set2)); 
?>

Output

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

Set1 elements are:
Ds\Set Object
(
    [0] => 81
    [1] => 82
    [2] => 83
)
Set2 elements are:
Ds\Set Object
(
    [0] => 84
    [1] => 85
    [2] => 86
)
The elements after the intersection:
object(Ds\Set)#3 (0) {
}

Example 3

In the example below, we use the intersect() function to create a new set that intersects the values with another set (['a', 'b', 'c', 'd']) −

<?php
$set1 = new \Ds\Set(['a', 'e', 'i', 'o', 'u']);
echo "Set1 elements are: \n";
print_r($set1);
$set2 = new \Ds\Set(['a', 'b', 'c', 'd']);
echo "Set2 elements are: \n";
print_r($set2);
echo "The elements after the intersection: \n";
var_dump($set1->intersect($set2)); 
?>

Output

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

Set1 elements are:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
Set2 elements are:
Ds\Set Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
The elements after the intersection:
object(Ds\Set)#3 (1) {
  [0]=>
  string(1) "a"
}
php_function_reference.htm
Advertisements