为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查数组是否在php中包含特定值?

如何检查数组是否在php中包含特定值?

PHP
繁华开满天机 2019-11-18 18:08:24
我有一个Array类型的PHP变量,我想找出它是否包含特定值,并让用户知道它在那里。这是我的数组:Array ( [0] => kitchen [1] => bedroom [2] => living_room [3] => dining_room) 我想做些类似的事情:if(Array contains 'kitchen') {echo 'this array contains kitchen';}最好的方法是什么?
查看完整描述

3 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

使用in_array()功能。


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


if (in_array('kitchen', $array)) {

    echo 'this array co


查看完整回答
反对 回复 2019-11-18
?
HUWWW

TA贡献1874条经验 获得超12个赞

// Once upon a time there was a farmer


// He had multiple haystacks

$haystackOne = range(1, 10);

$haystackTwo = range(11, 20);

$haystackThree = range(21, 30);


// In one of these haystacks he lost a needle

$needle = rand(1, 30);


// He wanted to know in what haystack his needle was

// And so he programmed...

if (in_array($needle, $haystackOne)) {

    echo "The needle is in haystack one";

} elseif (in_array($needle, $haystackTwo)) {

    echo "The needle is in haystack two";

} elseif (in_array($needle, $haystackThree)) {

    echo "The needle is in haystack three";

}


// The farmer now knew where to find his needle

// And he lived happily ever after


查看完整回答
反对 回复 2019-11-18
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

见in_array


<?php

    $arr = array(0 => "kitchen", 1 => "bedroom", 2 => "living_room", 3 => "dining_room");    

    if (in_array("kitchen", $arr))

    {

        echo sprintf("'kitchen' is in '%s'", implode(', ', $arr));

    }

?>


查看完整回答
反对 回复 2019-11-18
  • 3 回答
  • 0 关注
  • 497 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信