A set is a simply a collection of values without any particular order. It can contain any data type and we can run different set operations such as union, intersection, complement, and so on. As a set only contains values, we can construct a basic PHP array and assign values to it so that it grows dynamically. The following example shows two sets that we have defined; one contains some odd numbers and the other one has some prime numbers:
$odd = [];
$odd[] = 1;
$odd[] = 3;
$odd[] = 5;
$odd[] = 7;
$odd[] = 9;
$prime = [];
$prime[] = 2;
$prime[] = 3;
$prime[] = 5;
In order to check the existence of a value inside the set along with union, intersection, and complement operation, we can use the following example:
if (in_array(2, $prime)) {
echo "2 is a prime";
}
$union = array_merge($prime, $odd);
$intersection = array_intersect(...