Check if an array contains all array values from another array

Check if an array contains all array values from another array

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Checking if an Array Contains All Values from Another

Determining whether one array fully encompasses the values of another is a common requirement in data validation and filtering logic. Given the $all array containing integers like 307, 157, and 234, and a $search_this array with specific targets such as 200 and 234, you need a reliable method to verify if every element in $search_this exists within $all. The goal is simply to return a boolean result indicating success or failure.

The most straightforward approach leverages PHP’s built-in array_search() function combined with a loop or an array reduction. By iterating through each value in the search array and checking its existence in the main array, you can construct a logical condition that returns true only if no missing values are found. This method is efficient for small to medium-sized datasets and requires no external dependencies.

Here is a practical implementation using a loop:

$all = [
    0 => 307,
    1 => 157,
    2 => 234,
    3 => 200,
    4 => 322,
    5 => 324
];

$search_this = [
    0 => 200,
    1 => 234
];

$containsAll = true;

foreach ($search_this as $value) {
    if (array_search($value, $all) === false) {
        $containsAll = false;
        break;
    }
}

// $containsAll will be true if all values are found

This code iterates through each element in $search_this. For every value, it attempts to find a match in $all using array_search(). If array_search() returns false, it means the specific value is missing. At that point, we flag $containsAll as false and break out of the loop early. This short-circuit evaluation ensures we don't waste cycles checking remaining values once we know the condition has failed.

An alternative, more functional approach uses array_reduce() to accumulate a result across all search values. This version is often preferred in modern PHP codebases for its declarative style:

$containsAll = array_reduce($search_this, function ($carry, $item) use ($all) {
    return $carry && (array_search($item, $all) !== false);
}, true);

In this pattern, we start with a carry value of true. For each item in the search array, we check if it exists in $all. The reduction continues only as long as every check returns a truthy result. If any value is missing, the chain breaks and $containsAll becomes false.

Handling Duplicates and Key Associations

A critical detail when working with arrays in PHP is distinguishing between checking for keys versus values. In the examples provided, both arrays use numeric keys starting from zero. However, if $search_this contained duplicate values, say two instances of 234, and $all had only one instance, a simple value search might behave differently depending on whether you expect exact key matches or just value presence.

The array_search() function returns the first found key for a given value. If your requirement is strictly about value existence regardless of duplicates, the loop or reduce approach above handles it correctly by simply checking for truthiness. However, if you need to ensure that $all contains at least as many instances of a value as $search_this, you would need to use array_count_values() to compare frequencies before performing the check.

Performance Considerations

For large datasets, iterating through every element in $search_this against every element in $all becomes inefficient. The time complexity of array_search() is O(n), making the overall operation O(m * n), where m is the size of the search array and n is the size of the main array. To optimize this for performance, you should convert $all into an associative array or use a dedicated collection class like Laravel’s Collection.

If you are working within the Laravel ecosystem, converting the array to a collection allows for fluent chaining and optimized lookups:

use Illuminate\Support\Collection;

$collection = collect($all);
$search_this = [200, 234];

// Check if all items exist in the collection
$containsAll = $search_this->every(function ($value) use ($collection) {
    return $collection->contains($value);
});

This approach is particularly useful when integrating with existing Laravel models or Eloquent queries, where collections provide additional helper methods for filtering and mapping.

In summary, verifying array containment boils down to iterating through the search set and validating each element against the target set. Whether you choose a simple loop, a functional reduction, or a collection-based method, the core logic remains consistent: ensure no value in the search array is absent from the main array.

Our network