温馨提示×

如何判断一个值是否在php数组中

PHP
小樊
83
2024-06-30 00:33:37
栏目: 编程语言

在PHP中可以使用in_array()函数来判断一个值是否在数组中。示例如下:

$fruits = array("apple", "banana", "orange", "kiwi");

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

上面的代码会输出"apple is in the array",因为"apple"这个值在数组$fruits中。如果想判断一个值是否不在数组中,可以使用逻辑非运算符"!",示例如下:

if (!in_array("mango", $fruits)) {
    echo "mango is not in the array";
} else {
    echo "mango is in the array";
}

上面的代码会输出"mango is not in the array",因为"mango"这个值不在数组$fruits中。

0