This article mainly introduces PHP's use of the in_array function to check whether a value exists in the array. it gives a detailed analysis of the functions, definitions, usage techniques, and precautions of the in_array function, it has some reference value. if you need it, you can refer to the following example to describe 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.