In_array
(PHP 4, PHP 5)
in_array-checks if a value exists in the array
Description
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
<?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 appears as:
Got Irix
Example #2 In_array () strict type checking example
<?php $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 fo und with strict check\n "; }?>
The example above will output:
1.13 Found with strict check
Example #3 In_array () using Arrays as needle
<?php $a = array (' P ', ' h '), Array (' P ', ' r '), ' o '); if (In_array (' P ', ' h '), $a) {echo "' ph ' was found\n";} if (In_array (' f ', ' I '), $a) {echo "' fi ' is 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))