In_array
(PHP 4, PHP 5)
in_array-checks if a value exists in the array
Description
Copy CodeThe code is as follows: BOOL In_array (mixed $needle, array $haystack [, BOOL $strict])
Searches for needle in haystack and returns TRUE if found, otherwise FALSE.
If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack.
Note: If needle is a string, the comparison is case-sensitive.
Note: Needle is not allowed to be an array until PHP version 4.2.0.
Example #1 In_array () example
Copy CodeThe 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 appears as:
Got Irix
Example #2 In_array () strict type checking example
Copy CodeThe code is as follows:
$a = Array (' 1.10 ', 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 example above will output:
1.13 Found with strict check
Example #3 In_array () using Arrays as needle
Copy CodeThe code is as follows:
$a = array (' P ', ' h '), Array (' P ', ' r '), ' o ');
if (In_array (' P ', ' h '), $a)) {
echo "' ph ' was found\n";
}
if (In_array (Array (' f ', ' I '), $a)) {
echo "' fi ' was found\n";
}
if (In_array (' o ', $a)) {
echo "' O ' was found\n";
}
?>
The example above will output:
' ph ' was found
' O ' was found
places to be aware of:
If:
first declare an array as:
$arr = Array ( * );
Then there are:
In_array (0, $arr) = = True
puzzling! {Weak language}
Workaround:
In_array (strval (0), $arr, true))
http://www.bkjia.com/PHPjc/321552.html www.bkjia.com true http://www.bkjia.com/PHPjc/321552.html techarticle In_array (PHP 4, PHP 5) in_array-Check if there is a value in the array the copy code code is as follows: BOOL In_array (mixed $needle, array $haystack [, BOOL $strict]) in H ...