在 PHP 中,in_array() 函数用于检查一个数组中是否存在指定的值。如果该值存在,则返回 true,否则返回 false。
函数原型:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )参数说明:
$needle:要在数组中查找的值。$haystack:待搜索的数组。$strict:(可选)如果设置为 true,则在比较时会同时检查类型和值。默认为 false。示例:
$array = array("apple", "banana", "cherry");if (in_array("apple", $array)) { echo "apple exists in array";} else { echo "apple does not exist in array";}// 输出结果:apple exists in array使用严格模式进行比较:
$array = array("1", 2, 3);if (in_array(1, $array, true)) { echo "1 exists in array with strict mode";} else { echo "1 does not exist in array with strict mode";}// 输出结果:1 does not exist in array with strict mode 

