How to check if completely one element of an array exists in another array in php

I have two arrays, for example:

array1={1,2,3,4,5,6,7,8,9};
array2={4,6,9}

Is there any function so that I can determine what array2fully exists in array1?

I know that I can use a function in_array()in a loop, but in cases where I will have large arrays with hundreds of elements, so I'm looking for a function.

+3
source share
4 answers

Try:

$fullyExists = (count($array2) == count(array_intersect($array2, $array1));

The function returns only the elements of the second array that are present in all other arguments (only the first array in this case). So, if the intersection length is equal to the length of the second array, the second array completely contains the first. array_intersect.php

+10
source

array_intersect , .

, , ,

// The order of the arrays matters!
$isSubset = count(array_intersect($array2, $array1)) == count($array2);

, , , $array2 = array(4, 4). , array_unique:

$unique = array_unique($array2);
// The order of the arrays matters!
$isSubset = count(array_intersect($unique, $array1)) == count($unique);

, , , , array_intersect, . , $array2 $array1, , .

+1

:

 array_diff(array(1,2,3,4,5,6,7,8,9),array(4,6,9));

return - , , ,

0

,

var_dump(array(1,2,3,4,5,6,7,8,9) === array(4,6,9));
var_dump(array(1,2,3,4,5,6,7,8,9) === array(1,2,3,4,5,6,7,8,9));
0

All Articles