For more information about how to use the in_array function, see the basic tutorial for getting started with php.
Bool in_array (mixed $ needle, array $ haystack [, bool $ strict])
Search for needle in haystack. If yes, TRUE is returned. Otherwise, FALSE is returned.
If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack.
Note:
If the needle is a string, the comparison is case sensitive.
Note:
Before PHP version 4.2.0, needle cannot be an array.
Note: If the value parameter is a string and the type parameter is set to true, the search area is case sensitive.
Example #1 in_array ()
| The Code is as follows: |
Copy code |
<? Php
$ OS = array ("Mac", "NT", "Irix", "Linux ");
If (in_array ("Irix", $ OS )){
Echo "Got Irix ";
}
If (in_array ("mac", $ OS )){
Echo "Got mac ";
}
?>
|
The second condition fails, because in_array () is case sensitive, so the above program is shown:
Got Irix
Example #2 in_array () Strict type check Example
| The Code is as follows: |
Copy code |
|
<? Php
$ A = array ('1', 12.4, 1.13 );
If (in_array ('12. 4', $ a, true )){
Echo "'12. 4' found with strict checkn ";
}
If (in_array (1.13, $ a, true )){
Echo "1.13 found with strict checkn ";
}
?> |
Note: The in_array function returns 0, 1
When converted to boolean, the following values are considered FALSE:
Boolean value FALSE
Integer value 0 (0)
Floating point value: 0.0 (0)
Blank string and string "0"
Array without member variables
Objects without cells
Special Type NULL (including unset variables)
All other values are considered to be TRUE (including any resources ).
Warning
-1 is considered TRUE, like other non-zero values (both positive and negative values ).