如何检查数组是否包含 php 中的特定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8881676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if an array contains a specific value in php?
提问by hairynuggets
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 类型的 PHP 变量,我想知道它是否包含特定值并让用户知道它在那里。这是我的数组:
Array ( [0] => kitchen [1] => bedroom [2] => living_room [3] => dining_room)
and I would like do something like:
我想做类似的事情:
if(Array contains 'kitchen') {echo 'this array contains kitchen';}
What is the best way to do the above?
执行上述操作的最佳方法是什么?
回答by Wiseguy
Use the in_array()
function.
使用in_array()
函数。
$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');
if (in_array('kitchen', $array)) {
echo 'this array contains kitchen';
}
回答by Peter
// 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
回答by Abbas
回答by bobek
You need to use a search algorithm on your array. It depends on how large is your array, you have plenty of choices on what to use. Or you can use on of the built in functions:
您需要在数组上使用搜索算法。这取决于您的阵列有多大,您可以选择使用什么。或者您可以使用内置函数:
http://www.w3schools.com/php/php_ref_array.asp
http://www.w3schools.com/php/php_ref_array.asp
回答by crenate
From http://php.net/manual/en/function.in-array.php
来自http://php.net/manual/en/function.in-array.php
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Searches haystack for needle using loose comparison unless strict is set.
除非设置了严格,否则使用松散比较在大海捞针中搜索针。
回答by Eugen Rieck
if (in_array('kitchen', $rooms) ...
回答by antelove
Using dynamic variable for search in array
使用动态变量在数组中搜索
/* https://ideone.com/Pfb0Ou */
$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');
/* variable search */
$search = 'living_room';
if (in_array($search, $array)) {
echo "this array contains $search";
} else
echo "this array NOT contains $search";
回答by Pranav Rana
Following is how you can do this:
以下是您可以如何执行此操作:
<?php
$rooms = ['kitchen', 'bedroom', 'living_room', 'dining_room']; # this is your array
if(in_array('kitchen', $rooms)){
echo 'this array contains kitchen';
}
Make sure that you search for kitchenand not Kitchen. This function is case sensitive. So, the below function simply won't work:
确保您搜索的是kitchen而不是Kitchen。此函数区分大小写。所以,下面的函数根本不起作用:
$rooms = ['kitchen', 'bedroom', 'living_room', 'dining_room']; # this is your array
if(in_array('KITCHEN', $rooms)){
echo 'this array contains kitchen';
}
If you rather want a quick way to make this search case insensitive, have a look at the proposed solution in this reply: https://stackoverflow.com/a/30555568/8661779
如果您想要一种快速的方法使此搜索不区分大小写,请查看此回复中建议的解决方案:https: //stackoverflow.com/a/30555568/8661779
Source: http://dwellupper.io/post/50/understanding-php-in-array-function-with-examples
来源:http: //dwellupper.io/post/50/understanding-php-in-array-function-with-examples