166
$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.

2

5 Answers 5

329

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.

Sign up to request clarification or add additional context in comments.

8 Comments

What about of using empty(array_diff($search_this, $all));?
! operator on array will always return true. Use @Miguel 's suggestion: empty(array_diff($search_this, $all));
The original answer works because an empty array evaluates as false in PHP. Using empty() also works, but the ! operator does work fine.
@Miguel an empty array in this context is treated as falsey, there is no need to use empty.
@Sliq This question isn't about multidimensional arrays.
|
199

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

5 Comments

You know you can omit both count() calls?
@Wrikken Can't the values get reordered during array_intersect()? I mean, ['a', 'b'] != ['b', 'a'].
@exizt: array_intersect() does not alter the input arrays, so $search_this & $all are safe (it just returns an output). The function signature is array array_intersect ( array $array1 , array $array2 [, array $... ] ) (safe). If it would/could alter them, it would be array array_intersect ( array &$array1 , array &$array2 [, array &$... ] ) (possible altering of input arguments). Also, the keys of $search_this are preserve, and the order of the first array is kept. So, both key/value pairs, as their order, match.
And even then: array comparison: "== TRUE if $a and $b have the same key/value pairs.", so the order doesn't even matter (use === for that)
This answer assumes that the $all array only contains unique values. If this is not the case, one may use the array_unique function on the $all array in the array_intersects function.
15

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

Comments

7

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

Comments

0

How about this:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.