Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How check any value of array exist in another array php?

Tags:

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?

like image 539
EbinPaulose Avatar asked Aug 03 '12 05:08

EbinPaulose


People also ask

How do you check if all elements of an array exists in another array?

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.

How can I find matching values in two arrays PHP?

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.

How do you check if value exists in array of objects PHP?

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() .

How do you check if a value exists in an array?

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.


1 Answers

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.

like image 104
xbonez Avatar answered Oct 15 '22 02:10

xbonez