How to check if an array is a subset of another in PHP?

Description:

You need to find whether an array is subset of another array.

  • Let us suppose that there are two arrays.
  • First array is large which have 6 values.
  • Second array is small which have 2 values
  • Find if second array is subset of first which means that all values of second array should exists in first array.
  • You can use Decision Making Statements.

PHP Program to find if an array is a subset of another:

<?php
// Define two array
$array1 = array('a','1','2','3','4');
$array2 = array('a','3');

// intersect/matched values should be equal to first array
if (array_intersect($array2, $array1) === $array2) {
  echo "It is a subset";
} else {
   echo "Not a subset";
}
?>
Tutorials Class - Output Window

It is a subset



Learn more about the similar topics:
Tutorials
PHP Arrays
Exercises & Assignments
Remove specific element by value from an array in PHP?
PHP Array to String Conversion (favourite colours chosen by user)
How to check if an array is a subset of another in PHP?
Interview Questions & Answers
No Content Found.