What is the Ds\Set intersect() function in PHP?

The Ds\Set::intersect() function in PHP finds the intersection of two sets. The intersection of two sets is a set that contains the elements that are common in both sets.

Syntax

The Ds\Set::intersect() function can be declared as shown in the code snippet below:

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

Parameter

The Ds\Set::intersect() takes the following parameter:

  • set: The second set to take intersection with the first set.

Return value

The Ds\Set::intersect() function returns the intersection of the sets, i.e., a set containing both sets’ elements.

Example

Consider the code snippet below, which demonstrates the use of the Ds\Set::intersect() function:

<?php
$set1 = new \Ds\Set([1, 2, 3, 4]);
$set2 = new \Ds\Set([3, 4, 5, 6]);
echo("Intersection of set1 and set2: \n");
print_r($set1->intersect($set2));
?>

Explanation

Two sets set1 and set2 are declared in line 3 and line 5 respectively. The Ds\Set::intersect() function is used in line 9 to find intersection of set1 and set2.

Free Resources