I have two arrays : array("blue", "yellow")
and array("blue", "green", "red", "purple")
. Is there a function that will check if those two arrays have at least one element value in common ("blue") - just return true or false.
Use the inbuilt ES6 function some() to iterate through each and every element of first array and to test the array. Use the inbuilt function includes() with second array to check if element exist in the first array or not. If element exist then return true else return false.
The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.
$array1 = array("blue", "yellow");
$array2 = array("blue", "green", "red");
return count(array_intersect($array1, $array2)) > 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With