To check if a Set contains all the items of another Set in PHP, you can use the array_intersect
function.
In this example,
$set1
and $set2
.array_intersect
function to find the common elements between $set1
and $set2
.$set2
, then $set1
contains all items of $set2
.<?php
// Define Sets with items
$set1 = ["apple", "banana", "orange", "grape", "melon"];
$set2 = ["banana", "orange"];
// Check if set1 contains all items of set2
$containsAllItems = count(array_intersect($set1, $set2)) === count($set2);
// Print result
echo "Does set1 contain all items of set2? ".($containsAllItems ? 'true' : 'false');
?>
Does set1 contain all items of set2? true
In this example,
$set1
and $set2
.array_intersect
function to find the common elements between $set1
and $set2
.$set2
, then $set1
does not contain all items of $set2
.<?php
// Define Sets with items
$set1 = ["apple", "banana", "orange", "grape", "melon"];
$set2 = ["banana", "peach"];
// Check if set1 contains all items of set2
$containsAllItems = count(array_intersect($set1, $set2)) === count($set2);
// Print result
echo "Does set1 contain all items of set2? ".($containsAllItems ? 'true' : 'false');
?>
Does set1 contain all items of set2? false
In this tutorial, we learned How to check if a Set contains all the items of Another Set in PHP language with well detailed examples.