How can I use in_array if the needle is an array? How can I use in_array if the needle is an array? arrays arrays

How can I use in_array if the needle is an array?


Use array_diff():

$arr1 = array(1,2,3);$arr2 = array(1,2,3,4,5,6,7);$arr3 = array_diff($arr1, $arr2);if (count($arr3) == 0) {  // all of $arr1 is in $arr2}


You can use array_intersect or array_diff:

$arr1 = array(1,2,3);$arr2 = array(1,2,3,4,5,6,7);if ( $arr1 == array_intersect($arr1, $arr2) ) {    // All elements of arr1 are in arr2}

However, if you don't need to use the result of the intersection (which seems to be your case), it is more space and time efficient to use array_diff:

$arr1 = array(1,2,3);$arr2 = array(1,2,3,4,5,6,7);$diff = array_diff($arr1, $arr2);if ( empty($diff) ) {    // All elements of arr1 are in arr2}


You can try use the array_diff() function to find the difference between the two arrays, this might help you. I think to clarify you mean, all the values in the first array must be in the second array, but not the other way around.