温馨提示×

如何用php contains函数检查数组元素

PHP
小樊
113
2024-08-15 01:45:36
栏目: 编程语言

在PHP中,可以使用in_array()函数来检查数组中是否存在某个元素。示例如下:

$fruits = array("apple", "banana", "orange"); if (in_array("apple", $fruits)) { echo "apple is in the array"; } else { echo "apple is not in the array"; } 

上述代码中,首先定义了一个名为$fruits的数组,然后使用in_array()函数检查数组中是否存在"apple"元素。如果存在,则输出"apple is in the array";如果不存在,则输出"apple is not in the array"。

0