Skip to content

How to check if multiple element are in an array using PHP

Last updated on December 11, 2014

We generally use in_array function to check if an element is in array or not , but this function does not allow as to check multiple elements in the array. so in today’s post i just wanted to share small code snippets for checking multiple values in an array.

function my_in_array($search,$source) {
   return (count(array_intersect($search, $source)) == count($search));
}

Here array_intersect() function compare the values of two arrays, and return the matches, and that matches count should be equal to the $search array items count. if both are equal it will return true otherwise it will return false.

you can use the above function as shown below , it will return Boolean value.

$search = array(11,44,55);
$source = array(22,55,66,11,77,88,99,44);

var_dump(my_in_array($search,$source)); // return true;

$search = array(11,44,55);
$source = array(22,55,66,11,77,88,99,44);

var_dump(my_in_array($search,$source)); // return false;


That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments