In_array
(PHP 4, PHP 5)
In_array-check whether a value exists in the array
Description
Copy codeThe Code is as follows: 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 needle is a string, the comparison is case sensitive.
Note: Before PHP version 4.2.0, needle cannot be an array.
Example #1 in_array ()
Copy codeThe Code is as follows:
<? 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
Copy codeThe Code is as follows:
<? Php
$ A = array ('1', 12.4, 1.13 );
If (in_array ('12. 4', $ a, true )){
Echo "'12. 4' found with strict check \ n ";
}
If (in_array (1.13, $ a, true )){
Echo "1.13 found with strict check \ n ";
}
?>
The above example will output:
1.13 found with strict check
Example #3 in_array () using arrays as needle
Copy codeThe Code is as follows:
<? Php
$ A = array ('P', 'H'), array ('P', 'R'), 'O ');
If (in_array (array ('P', 'H'), $ )){
Echo "'ph' was found \ n ";
}
If (in_array (array ('F', 'I'), $ )){
Echo "'fi 'was found \ n ";
}
If (in_array ('O', $ )){
Echo "'O' was found \ n ";
}
?>
The above example will output:
'Ph 'was found
'O' was found
Note:
Suppose:
Declare an array as follows:
$ Arr = array ( * );
Then there are:
In_array (0, $ arr) = true
Confusing!{Weak language}
Solution:
In_array (strval (0), $ arr, true ))