我有一个类型为Array的PHP变量,我想知道它是否包含特定值并让用户知道它在那里.这是我的阵列:
Array ( [0] => kitchen [1] => bedroom [2] => living_room [3] => dining_room)
Run Code Online (Sandbox Code Playgroud)
我想做点什么:
if(Array contains 'kitchen') {echo 'this array contains kitchen';}
Run Code Online (Sandbox Code Playgroud)
做上述事情的最佳方法是什么?
Wis*_*guy 130
使用该in_array()功能.
$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');
if (in_array('kitchen', $array)) {
echo 'this array contains kitchen';
}
Run Code Online (Sandbox Code Playgroud)
Pet*_*ter 15
// 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
Run Code Online (Sandbox Code Playgroud)
Abb*_*bas 11
<?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));
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
128443 次 |
| 最近记录: |