Description of phpin_array and in_array functions. In_array (PHP4, PHP5) in_array-check whether a value exists in the array. The Copy code is as follows: boolin_array (mixed $ needle, array $ haystack [, bool $ strict]) at h in_array
(PHP 4, PHP 5)
In_array-check whether a value exists in the array
Description
The 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 ()
The code is as follows:
$ 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:
$ 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
The code is as follows:
$ 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 ))
Http://www.bkjia.com/PHPjc/321552.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321552.htmlTechArticlein_array (PHP 4, PHP 5) in_array-check if there is a value in the array description code: bool in_array (mixed $ needle, array $ haystack [, bool $ strict]) in h...