How to create a Set of Sets in PHP - Step by Step Examples
How to create a Set of Sets in PHP ?
Answer
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.
✐ Examples
1 Creating a Set of Sets of Integers
In this example,
- First, we create three individual sets of integers named
$set1,$set2, and$set3. We add the elements1, 2, 3to$set1,4, 5, 6to$set2, and7, 8, 9to$set3. - Next, we create an array named
$setOfSetsand add$set1,$set2, and$set3to it. This nests the three sets within a single array. - We then iterate over each set within
$setOfSetsusing a foreach loop. Within the loop, we print each individual set to the standard output.
PHP Program
<?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
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 4
[1] => 5
[2] => 6
)
Array
(
[0] => 7
[1] => 8
[2] => 9
)
2 Creating a Set of Sets of Strings
In this example,
- First, we create three individual sets of strings named
$setA,$setB, and$setC. We add the elements"apple", "banana", "cherry"to$setA,"dog", "elephant", "fox"to$setB, and"grape", "honeydew", "kiwi"to$setC. - Next, we create an array named
$setOfStringSetsand add$setA,$setB, and$setCto it. This nests the three sets within a single array. - We then iterate over each set within
$setOfStringSetsusing a foreach loop. Within the loop, we print each individual set to the standard output.
PHP Program
<?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
Output:
Array
(
[0] => apple
[1] => banana
[2] => cherry
)
Array
(
[0] => dog
[1] => elephant
[2] => fox
)
Array
(
[0] => grape
[1] => honeydew
[2] => kiwi
)
Summary
In this tutorial, we learned How to create a Set of Sets in PHP language with well detailed examples.
More PHP Sets Tutorials
- How to create an Empty Set in PHP ?
- How to Initialize a Set in PHP ?
- How to Get Length of a Set in PHP ?
- How to create a Set of size N in PHP ?
- How to create a Set of Numbers from 1 to N in PHP ?
- How to create a Set of integers in PHP ?
- How to create a Set of Strings in PHP ?
- How to Access Items in a Set in PHP ?
- How to get a Random Item in a Set in PHP ?
- How to Iterate Over a Set in PHP ?
- How to check if a Set is Empty in PHP ?
- How to check if a Set is Not Empty in PHP ?
- How to get Subset from a Set in PHP ?
- How to check if a Specific Item is present in the Set in PHP ?
- How to check if a Set contains all the items of Another Set in PHP ?
- How to Sort Items of a Set in PHP ?
- How to Copy a Set in PHP ?
- How to add an Item to a Set in PHP ?
- How to find Union of Two Sets in PHP ?
- How to find Intersection of Two Sets in PHP ?
- How to check if Two Sets are Equal in PHP ?
- How to Convert a Set of Integers to a Set of Strings in PHP ?
- How to Convert a Set of Strings to a Set of Integers in PHP ?
- How to Convert a Set of Floats to a Set of Strings in PHP ?
- How to Convert a Set of Strings to a Set of Floats in PHP ?
- How to Filter Items of a Set based on a Condition in PHP ?
- How to Remove Specific Item from a Set in PHP ?
- How to Remove Items from Set based on a Condition in PHP ?
- How to create a Set of Sets in PHP ?