如何检查数组是否包含 php 中的特定值? [英] How can I check if an array contains a specific value in php?

查看:22
本文介绍了如何检查数组是否包含 php 中的特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组类型的 PHP 变量,我想知道它是否包含特定值并让用户知道它在那里.这是我的数组:

I have a PHP variable of type Array and I would like find out if it contains a specific value and let the user know that it is there. This is my array:

Array ( [0] => kitchen [1] => bedroom [2] => living_room [3] => dining_room) 

我想做类似的事情:

if(Array contains 'kitchen') {echo 'this array contains kitchen';}

执行上述操作的最佳方法是什么?

What is the best way to do the above?

推荐答案

使用in_array()函数.

$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');

if (in_array('kitchen', $array)) {
    echo 'this array contains kitchen';
}

这篇关于如何检查数组是否包含 php 中的特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆