如何判断一个值是否在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中。

Both comments and pings are currently closed.

Comments are closed.

Powered by KingAbc | 粤ICP备16106647号-2 | Loading Time‌ 0.217