PHP: Check if an array contains all array values from another array

You can use the array_diff function in PHP to compare two arrays and check if the first array contains all of the values from the second array.

<?php

$arr1 = ["a", "b", "c"];
$arr2 = ["a", "b"];

if (empty(array_diff($arr2, $arr1))) {
  echo "arr1 contains all values from arr2";
} else {
  echo "arr1 does not contain all values from arr2";
}

Watch a course Learn object oriented PHP

You can also use array_intersect to check if all elements of second array are present in first array.

<?php

$arr1 = [1, 2, 3, 4, 5];
$arr2 = [3, 5, 1];

if (count(array_intersect($arr2, $arr1)) == count($arr2)) {
  echo "All values from arr2 are present in arr1.";
} else {
  echo "Not all values from arr2 are present in arr1.";
}