PHP in_array() function
Example
Search for the value "Glenn" in an array and output some text:
<?php $people = array("Bill", "Steve", "Mark", "David"); if (in_array("Mark", $people)) { echo "Match found"; } else { echo "Match not found"; } ?>
Definition and Usage
The in_array() function searches for a specified value in an array.
Note:if search The parameter is a string and type If the parameter is set to TRUE, the search is case-sensitive.
Syntax
in_array(search,array,type)
Parameter | Description |
---|---|
search | Required. Specifies the value to be searched in the array. |
array | Required. Specifies the array to be searched. |
type | Optional. If the parameter is set to true, it checks whether the search data and the type of the array values are the same. |
Description
If the given value search exists in the array array then returns true. If the third parameter is set to true, the function returns true only if the element exists in the array and the data type is the same as the given value.
If the parameter is not found in the array, the function returns false.
Note:if search The parameter is a string and type If the parameter is set to true, the search is case-sensitive.
Technical Details
Return Value: | Returns TRUE if the value is found in the array, otherwise returns FALSE. |
PHP Version: | 4+ |
Changelog: | Since PHP 4.2,search The parameters may also be an array now. |
More Examples
Example 1
Use all parameters:
<?php $people = array("Bill", "Steve", "Mark", "David"); if (in_array("23", $people, TRUE)) { echo "Match found<br>"; } else { echo "Match not found<br>"; } if (in_array("Mark",$people, TRUE)) { echo "Match found<br>"; } else { echo "Match not found<br>"; } if (in_array(23,$people, TRUE)) { echo "Match found<br>"; } else { echo "Match not found<br>"; } ?>