当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > in_array() 函数
The in_array() function searches an array for a specific value.
in_array()函数的作用是:在数组中搜索指定的值。
This function returns TRUE if the value is found in the array, or FALSE otherwise.
如果这个值存在于数组中,该函数将返回True,否则将返回False。
in_array(search,array,type) |
Parameter参数 | Description描述 |
---|---|
search | Required. Specifies the what to search for 必要参数。指定需要搜索的值 |
array | Required. Specifies the array to search 必要参数。指定在哪个数组中进行搜索 |
type | Optional. If this parameter is set, the in_array() function searches for the search-string and specific type in the array 可选参数。如果设置这个参数,那么in_array()函数将搜索数组中指定要搜索的字符串和详细的类型 |
Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
注意:如果搜索(search)参数是一个字符串,并且type参数设置为TRUE,那么整个搜索将是区分大小写的。
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); if (in_array("Glenn",$people)) { echo "Match found"; } else { echo "Match not found"; } ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Match found |
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland", 23); if (in_array("23",$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } if (in_array("Glenn",$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 />"; } ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Match not found Match found Match not found |