11

Check if array contains all values of another array in PHP

 1 year ago
source link: https://thispointer.com/check-if-array-contains-all-values-of-another-array-in-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

This tutorial will discuss about unique ways to check if array contains all values from another array in php.

Table Of Contents

Method 1: Using array_diff() function

Suppose we have two arrays,

Copy to clipboard
$arr1 = [11, 56, 43];
$arr2 = [21, 22, 56, 33, 43, 11];
$arr1 = [11, 56, 43];
$arr2 = [21, 22, 56, 33, 43, 11];

now we want to check if all the elements of the first array exist in the second array or not. For that we will pass both the array into the array_diff() function, and it will return a new array containing only those values which are present in first array but not present in the second array. Then we will check if this returned array is empty or not. If yes, then it means all the values from the first area are present in the second array.

Let’s see the complete example,

Advertisements

Copy to clipboard
<?php
$arr1 = [11, 56, 43];
$arr2 = [21, 22, 56, 33, 43, 11];
// Get the difference between the two arrays
// i.e. values which are in arr1 but not in arr2
$diff = array_diff($arr1, $arr2);
// Check the diff array is empty
if (empty($diff)) {
echo "Yes, all elements of first array are present in second array";
} else {
echo "No, all elements of first array are not present in the second array";
?>
<?php
$arr1 = [11, 56, 43];
$arr2 = [21, 22, 56, 33, 43, 11];

// Get the difference between the two arrays
// i.e. values which are in arr1 but not in arr2
$diff = array_diff($arr1, $arr2);

// Check the diff array is empty
if (empty($diff)) {
    echo "Yes, all elements of first array are present in second array";
} else {
    echo "No, all elements of first array are not present in the second array";
}
?>

Output

Copy to clipboard
Yes, all elements of first array are present in second array
Yes, all elements of first array are present in second array

Method 2: Using foreach() function

Using foreach() function, we can iterate over all the elements of the first array and during iteration we will check each element exists in the second array or not. As soon as we find an element that does not exist in the second array, we will mark the result value as false and break the loop. At the end of the loop if the result value is true, that it means all the elements of first are present in the second array.

Let’s see the complete example,

Copy to clipboard
<?php
$arr1 = [11, 56, 43];
$arr2 = [21, 22, 56, 33, 43, 11];
$result = true;
// Loop through all elements of $arr1 and
// check if it is present in $arr2
foreach ($arr1 as $elem) {
if (!in_array($elem, $arr2)) {
$result = false;
break;
if ($result) {
echo "Yes, all elements of first array are present in second array";
} else {
echo "No, all elements of first array are not present in the second array";
?>
<?php
$arr1 = [11, 56, 43];
$arr2 = [21, 22, 56, 33, 43, 11];

$result = true;

// Loop through all elements of $arr1 and
// check if it is present in $arr2
foreach ($arr1 as $elem) {
    if (!in_array($elem, $arr2)) {
        $result = false;
        break;
    }
}

if ($result) {
  echo "Yes, all elements of first array are present in second array";
} else {
  echo "No, all elements of first array are not present in the second array";
}
?>

Output

Copy to clipboard
Yes, all elements of first array are present in second array
Yes, all elements of first array are present in second array

Summary

We learned about two ways to check if all elements of first array are present in second array in PHP. Thanks.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK