Check Whether an Array is Subset of Another Array in PHP
Last Updated :
31 Jul, 2024
Given two arrays i.e. subset and superset, the task is to check whether an array is subset of another array in PHP. This article covers various methods to determine whether an array is a subset of another array.
These are the following approaches:
Using array_diff() Function
The array_diff() function computes the difference of arrays. If the result of the difference between the subset array and the superset array is empty, it means all elements of the subset are present in the superset.
- array_diff($subset, $superset) returns an array containing elements in $subset that are not in $superset.
- If this difference is empty, $subset is a subset of $superset.
Example: This example shows the checking of if an array is a subset of another array using array_diff() function.
PHP
<?php
function isSubset($subset, $superset) {
return empty(array_diff($subset, $superset));
}
// Driver code
$subset = [1, 2, 3];
$superset = [1, 2, 3, 4, 5];
if (isSubset($subset, $superset)) {
echo "Array is a subset";
} else {
echo "Array is not a subset";
}
?>
Using array_intersect() Function
The array_intersect() function returns an array of values that are present in all the input arrays. If the intersection of the subset array and the superset array has the same number of elements as the subset array, then the subset is entirely contained within the superset.
- array_intersect($subset, $superset) returns an array containing elements found in both $subset and $superset.
- If the count of this intersection equals the count of $subset, then $subset is a subset of $superset.
Example: This example shows the checking of if an array is a subset of another array using array_intersect() function.
PHP
<?php
function isSubset($subset, $superset) {
return count(array_intersect($subset, $superset)) == count($subset);
}
// Driver code
$subset = [1, 2, 3];
$superset = [1, 2, 3, 4, 5];
if (isSubset($subset, $superset)) {
echo "Array is a subset";
} else {
echo "Array is not a subset";
}
?>
Using Loops and in_array() Function
A more manual approach involves looping through each element in the subset array and checking if it exists in the superset array using in_array() function.
- Iterate through each element in $subset.
- Use in_array() function to check if the element is in $superset.
- If any element is not found, return false. If all elements are found, return true.
Example: This example shows the checking of if an array is a subset of another array using lops and in_array_diff() function.
PHP
<?php
function isSubset($subset, $superset) {
foreach ($subset as $element) {
if (!in_array($element, $superset)) {
return false;
}
}
return true;
}
// Driver code
$subset = [1, 2, 3];
$superset = [1, 2, 3, 4, 5];
if (isSubset($subset, $superset)) {
echo "Array is a subset";
} else {
echo "Array is not a subset";
}
?>
Using array_flip() and array_key_exists() Functions
The array_flip() function flips the keys and values of an array. By flipping the superset array and then checking if each element in the subset array exists as a key in the flipped superset array using array_key_exists(), we can determine if the subset is contained within the superset.
Steps:
- Flip the superset array using array_flip().
- Iterate through each element in the subset array.
- Use array_key_exists() to check if the element exists in the flipped superset array.
- If any element is not found, return false. If all elements are found, return true.
Example: This example shows the checking of if an array is a subset of another array using array_flip() and array_key_exists() functions.
PHP
<?php
function isSubset($subset, $superset) {
$flippedSuperset = array_flip($superset);
foreach ($subset as $element) {
if (!array_key_exists($element, $flippedSuperset)) {
return false;
}
}
return true;
}
$subset = [1, 2, 3];
$superset = [1, 2, 3, 4, 5];
if (isSubset($subset, $superset)) {
echo "Array is a subset";
} else {
echo "Array is not a subset";
}
?>
Output
Array is a subset
OutputArray is a subset
Output
Array is a subset
Similar Reads
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read
How to Filter an Array from all Elements of another Array In JavaScript? Filtering one array from all elements of another array is a common task in JavaScript. This involves removing elements from one array that are present in another array. In this article we will explore different approaches to filter an array from all elements of another array in JavaScript.These are
4 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to check if a value exists in an Array in Ruby? In this article, we will discuss how to check if a value exists in an array in ruby. We can check if a value exists in an array through different methods ranging from using Array#member? method and Array#include? method to Array#any? method Table of Content Check if a value exists in an array using
3 min read
PHP | Deleting an element from array using array_diff() Given an array, we have to delete an element from that array using the array_diff() function in PHP.Examples:Input : $arr = array(2, 8, 9, 7, 6, 5); $arr = array_diff($arr, array(9)); Output : Array ( [0] => 2 [1] => 8 [3] => 7 [4] => 6 [5] => 5 ) Input : $arr = array("shubham", "aksh
2 min read
How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he
2 min read
How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
How to Find Union and Intersection of Two Unsorted Arrays in PHP? Given two unsorted arrays representing two sets (elements in every array are distinct), find the union and intersection of two arrays in PHP. Example: Input: arr1[] = {7, 1, 5, 2, 3, 6} , arr2[] = {3, 8, 6, 20, 7} Output: Union {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6, 7}These are the fol
4 min read
PHP Merging two or more arrays using array_merge() The array_merge() function in PHP combines two or more arrays into one. It merges the values from the arrays in the order they are passed. If arrays have duplicate string keys, the latter values overwrite earlier ones, while numerical keys are re-indexed sequentially.Syntaxarray array_merge($array1,
2 min read
How to reset Array in PHP ? You can reset array values or clear the values very easily in PHP. There are two methods to reset the array which are discussed further in this article. Methods: unset() Functionarray_diff() Function Method 1: unset() function: The unset() function is used to unset a specified variable or entire arr
2 min read