PHP uses the in_array function to check whether a value exists in the array. The in_array Array
This example describes how PHP uses the in_array function to check whether a value exists in the array. Share it with you for your reference. The specific analysis is as follows:
PHP uses the in_array () function to check whether a value exists in the array. If yes, TRUE is returned; otherwise, FALSE is returned, which is very useful. Next I will introduce in_array () in depth () function.
Recently, when writing a piece of code in php, You need to determine whether a value is in another set of values. The in_array function is used to check whether a value exists in the array. The concept is rather vague. You can use specific examples to understand its functions.
Syntax:
bool in_array( mixed needle, array array [, bool strict] )
Parameter description:
Parameters |
Description |
Needle |
The value to be searched in the array. If it is a string, it is case sensitive. |
Array |
Array to be retrieved |
Strict |
Optional. If this parameter is set to TRUE, the needle and array value types are checked. |
Example 1:
<?php$os = array("Mac", "NT", "Irix", "Linux");if (in_array("Irix", $os)) { echo "Got Irix";}if (in_array("mac", $os)) { echo "Got mac";}?>
The execution result of the above Code is:
Got Irix
The second condition fails because in_array () is case sensitive.
Example 2:
<? Php $ europe = array ("USA", "UK", "France", "Germany", "Italy", "Spain", "Denmark "); if (in_array ("us", $ europe) {echo "True" ;}?>
Same as above, the execution result is True.
Example 3: strict type check
<?php$a = array('1.10', 12.4, 1.13);if (in_array('12.4', $a, true)) { echo "'12.4' found with strict check ";}if (in_array(1.13, $a, true)) { echo "1.13 found with strict check ";}?>
The output result is:
1.13 found with strict check
Example 4: Apply an array to an array
<?php$a = array(array('p', 'h'), array('p', 'r'), 'o');if (in_array(array('p', 'h'), $a)) { echo "'ph' was found ";}if (in_array(array('f', 'i'), $a)) { echo "'fi' was found ";}if (in_array('o', $a)) { echo "'o' was found ";}?>
The output result is:
'Ph 'was found
'O' was found
Its usage is as follows:
bool in_array(mixed $needle,array $haystack [, bool $strict = FALSE ])
Search for needle in haystack. If strict is not set, use loose comparison.
Note: Since php5.4. The array definition is changed from array () to array [].
I hope this article will help you with php programming.