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

PhpArrays

Php Problem Overview


$all = array
(
    0 => 307,
    1 => 157,
    2 => 234,
    3 => 200,
    4 => 322,
    5 => 324
);
$search_this = array
(
    0 => 200,
    1 => 234
);

I would like to find out if $all contains all $search_this values and return true or false. Any ideas please?

Php Solutions


Solution 1 - Php

The previous answers are all doing more work than they need to. Just use array_diff. This is the simplest way to do it:

$containsAllValues = !array_diff($search_this, $all);

That's all you have to do.

Solution 2 - Php

Look at array_intersect().

$containsSearch = count(array_intersect($search_this, $all)) === count($search_this);

Or for associative array, look at array_intersect_assoc().

Or for recursive compare of sub-arrays, try:

<?php

namespace App\helpers;

class Common {
    /**
     * Recursively checks whether $actual parameter includes $expected.
     *
     * @param array|mixed $expected Expected value pattern.
     * @param array|mixed $actual Real value.
     * @return bool
     */
    public static function intersectsDeep(&$expected, &$actual): bool {
        if (is_array($expected) && is_array($actual)) {
            foreach ($expected as $key => $value) {
                if (!static::intersectsDeep($value, $actual[$key])) {
                    return false;
                }
            }
            return true;
        } elseif (is_array($expected) || is_array($actual)) {
            return false;
        }
        return (string) $expected == (string) $actual;
    }
}

Solution 3 - Php

A bit shorter with array_diff

$musthave = array('a','b');
$test1 = array('a','b','c');
$test2 = array('a','c');

$containsAllNeeded = 0 == count(array_diff($musthave, $test1));

// this is TRUE

$containsAllNeeded = 0 == count(array_diff($musthave, $test2));

// this is FALSE

Solution 4 - Php

I think you're looking for the intersect function

array array_intersect ( array $array1 , array $array2 [, array $ ... ] )

array_intersect() returns an array containing all values of array1 that are present in all the arguments. Note that keys are preserved.

http://www.php.net/manual/en/function.array-intersect.php

Solution 5 - Php

How about this:

function array_keys_exist($searchForKeys = array(), $searchableArray) {
    $searchableArrayKeys = array_keys($searchableArray);

    return count(array_intersect($searchForKeys, $searchableArrayKeys)) == count($searchForKeys); 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeterView Question on Stackoverflow
Solution 1 - PhporrdView Answer on Stackoverflow
Solution 2 - PhpjasonbarView Answer on Stackoverflow
Solution 3 - PhpjavigzzView Answer on Stackoverflow
Solution 4 - PhpJames KyburzView Answer on Stackoverflow
Solution 5 - PhpK-AlexView Answer on Stackoverflow