in_array() 函数用于检测一个数组中是否存在某个值。它会在数组中搜索指定的值,并根据检索结果返回 true 或 false。它的语法格式如下:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
其中,needle
参数是需要搜索的值,haystack
参数是被搜索的数组,strict
参数是一个可选参数,它指定搜索时是否使用严格模式(类型和数值也要匹配才算存在)。
例如,以下代码将搜索一个数组中是否存在值为 "apple" 的元素:
$fruits = array("apple", "banana", "orange");
if (in_array("apple", $fruits)) {
echo "Found "apple" in the array";
}
输出结果为:
Found "apple" in the array