[php] 배열에 다른 배열의 모든 배열 값이 포함되어 있는지 PHP 확인

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

$ all에 모든 $ search_this 값이 포함되어 있고 true 또는 false를 반환하는지 확인하고 싶습니다. 제발 어떤 생각?



답변

array_intersect () 보세요 .

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


답변

이전 답변은 모두 필요한 것보다 더 많은 작업을 수행하고 있습니다. array_diff를 사용하십시오 . 이것이 가장 간단한 방법입니다.

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

그게 당신이해야 할 전부입니다.


답변

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


답변

나는 당신이 교차 기능을 찾고 있다고 생각합니다

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

array_intersect()모든 인수에있는 array1의 모든 값을 포함하는 배열을 반환합니다. 키는 유지됩니다.

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


답변

이건 어때요:

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

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


답변