To create a Set of Sets in PHP, you can use arrays to represent sets. You create individual sets as arrays and then add them to another array.
In this example,
$set1
, $set2
, and $set3
. We add the elements 1, 2, 3
to $set1
, 4, 5, 6
to $set2
, and 7, 8, 9
to $set3
.$setOfSets
and add $set1
, $set2
, and $set3
to it. This nests the three sets within a single array.$setOfSets
using a foreach loop. Within the loop, we print each individual set to the standard output.<?php
$set1 = [1, 2, 3];
$set2 = [4, 5, 6];
$set3 = [7, 8, 9];
$setOfSets = [$set1, $set2, $set3];
foreach ($setOfSets as $set) {
print_r($set);
}
?>
Output: Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 4 [1] => 5 [2] => 6 ) Array ( [0] => 7 [1] => 8 [2] => 9 )
In this example,
$setA
, $setB
, and $setC
. We add the elements "apple", "banana", "cherry"
to $setA
, "dog", "elephant", "fox"
to $setB
, and "grape", "honeydew", "kiwi"
to $setC
.$setOfStringSets
and add $setA
, $setB
, and $setC
to it. This nests the three sets within a single array.$setOfStringSets
using a foreach loop. Within the loop, we print each individual set to the standard output.<?php
$setA = ["apple", "banana", "cherry"];
$setB = ["dog", "elephant", "fox"];
$setC = ["grape", "honeydew", "kiwi"];
$setOfStringSets = [$setA, $setB, $setC];
foreach ($setOfStringSets as $set) {
print_r($set);
}
?>
Output: Array ( [0] => apple [1] => banana [2] => cherry ) Array ( [0] => dog [1] => elephant [2] => fox ) Array ( [0] => grape [1] => honeydew [2] => kiwi )
In this tutorial, we learned How to create a Set of Sets in PHP language with well detailed examples.