Php check if any value in array is in another array
Here's a way I am doing it after researching it for a while. I wanted to make a Laravel API endpoint that checks if a field is "in use", so the important information is: 1) which DB table? 2) what DB column? and 3) is there a value in that column that matches the search terms? Show Knowing this, we can construct our associative array:
Then, we can set our values that we will check:
Then, we can use
I apologize for the Laravel-specific PHP code, but I will leave it because I think you can read it as pseudo-code. The important part is the two
source:
The nice thing about the algorithm that I showed above is that you can make a REST endpoint such as You could have:
and then you could make GET requests such as:
Notice also that no one can do:
Good luck on your journey. (PHP 4, PHP 5, PHP 7, PHP 8) in_array — Checks if a value exists in an array Descriptionin_array(mixed Parametersneedle The searched value.
haystack The array. strict If the third parameter
Return Values Returns ExamplesExample #1 in_array() example
The second condition fails because in_array() is case-sensitive, so the program above will display: Example #2 in_array() with strict example
in_array('12.4', $a, true)) { The above example will output: 1.13 found with strict check Example #3 in_array() with an array as needle
in_array(array('p', 'h'), $a)) { The above example will output: 'ph' was found 'o' was found See Also
beingmrkenny at gmail dot com ¶ 10 years ago
in_array(null, $array); // true rhill at xenu-directory dot net ¶ 13 years ago
= array( thomas dot sahlin at gmail dot com ¶ 12 years ago
= array('apple', 'banana', 'orange'); How do you check if an array contains a value from another array?How to Check if Java Array Contains a Value?. Using For Loop.. Using List contains() method.. Using Stream anyMatch() Method.. Arrays binarySearch() for sorted array.. Checking if Array Contains Multiple Values.. References.. How do I check if an array contains another array in PHP?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.
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 an array contains 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.
|