PHP uses the in_array function to check whether a value exists in the array. PHP uses the in_array function to check whether a value exists in the array. This article mainly introduces PHP's use of the in_array function to check whether a value exists in the array, the in_array function is used by PHP to check whether a value exists in the array.
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. For more information, see
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:
?
1 |
Bool in_array (mixed needle, 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:
?
1 2 3 4 5 6 7 8 9 |
$ 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:
?
1 2 3 4 5 6 |
$ Europe = array ("USA", "UK", "France", "Germany", "Italy", "Spain", "Denmark "); If (in_array ("USA", $ europe )){ Echo "True "; } ?> |
Same as above, the execution result is True.
Example 3: strict type check
?
1 2 3 4 5 6 7 8 9 |
$ A = array ('1', 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
?
1 2 3 4 5 6 7 8 9 10 11 12 |
$ A = array ('P', 'H'), array ('P', 'r'), 'o '); If (in_array (array ('P', 'H'), $ )){ Echo "'ph' was found "; } If (in_array (array ('F', 'I'), $ )){ Echo "'Fi 'was found "; } If (in_array ('O', $ )){ Echo "'O' was found "; } ?> |
The output result is:
'Ph 'was found
'O' was found
Its usage is as follows:
?
1 |
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.
This article describes how to use the in_array function in PHP to check whether a value exists in the array. the in_array function is analyzed in detail...