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

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

问题描述

我有一个表格,创建一个数据数组,我想看看它是否包含一个特定的值,让用户知道它的存在。这是我的数组:

I have an array of data created by a form that 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天全站免登陆