PHP in_array() 函数
实例
在数组中搜索值 "Glenn" ,并输出一些文本:
<?php $people = array("Bill", "Steve", "Mark", "David"); if (in_array("Mark", $people)) { echo "匹配已找到"; } else { echo "匹配未找到"; } ?>
定义和用法
in_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: | If the value is found in the array, it returns TRUE, otherwise it 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>"; } ?>