I have two array
$a = array('a','b'); $b = array('a','1','2','3','4');
How to checks any value of array $a exists in array $b without using loop?
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 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.
The function in_array() returns true if an item exists in an array. You can also use the function array_search() to get the key of a specific item in an array. In PHP 5.5 and later you can use array_column() in conjunction with array_search() .
You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.
if (count(array_intersect($array1, $array2)) === 0) { // No values from array1 are in array 2 } else { // There is at least one value from array1 present in array2 }
http://php.net/manual/en/function.array-intersect.php
Probably worth nothing that, in all likelihood, under the hood, a loop is used.
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