PHP

php如何检查数组中是否存在某个值

小亿
88
2024-05-30 18:36:07
栏目: 编程语言
PHP开发者专用服务器,限时0元免费领! 查看>>

要检查数组中是否存在某个值,可以使用PHP中的in_array()函数。该函数接受两个参数:要检查的值以及要搜索的数组。如果值存在于数组中,函数将返回true,否则返回false。

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

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

在上面的例子中,我们检查了数组$fruits是否包含值"banana",如果包含则输出"Banana is in the array",否则输出"Banana is not in the array"。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:php怎么检查数组中是否存在某个值

0
看了该问题的人还看了