W3docs

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.

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.

Example of using the array_diff function in PHP to compare two arrays

<?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";
}

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

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

Example of using the array_intersect function in PHP to compare two arrays

<?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.";
}