扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

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

扬州沐宇科技
2024-06-30 00:33:37
PHP

在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中。

扫码添加客服微信