Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if one array elements entirely exists in another array in php [duplicate]

Tags:

arrays

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 that array2 fully exists in array1?

I know i can use the in_array() function in a loop but in cases where I will have large arrays with hundreds of elements so I am searching for a function.

like image 912
Sumit Neema Avatar asked Dec 16 '22 00:12

Sumit Neema


1 Answers

Try:

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

The array_intersect.php function will return only elements of the second array that are present in all the other arguments (only the first array in this case). So, if the length of the intersection is equal to the lenght of the second array, the second array is fully contained by the first one.

like image 189
lorenzo-s Avatar answered Dec 21 '22 23:12

lorenzo-s