php 如何检查数组值是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2440506/
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 to check if an array value exists?
提问by Uffo
How can I check if $something['say']has the value of 'bla'or 'omg'?
如何检查是否$something['say']具有'bla'或的值'omg'?
$something = array('say' => 'bla', 'say' => 'omg');
回答by Benjamin Ortuzar
回答by Tatu Ulmanen
Using if?
使用if?
if(isset($something['say']) && $something['say'] == 'bla') {
// do something
}
Btw, you are assigning an value with the key saytwice, hence your array will result in an array with only one value.
顺便说一句,您使用键say两次分配一个值,因此您的数组将导致一个只有一个值的数组。
回答by Neeraj Singh
Using:in_array()
使用:in_array()
$search_array = array('user_from','lucky_draw_id','prize_id');
if (in_array('prize_id', $search_array)) {
echo "The 'prize_id' element is in the array";
}
Here is output:The 'prize_id' element is in the array
这是输出:The 'prize_id' element is in the array
Using:array_key_exists()
使用:array_key_exists()
$search_array = array('user_from','lucky_draw_id','prize_id');
if (array_key_exists('prize_id', $search_array)) {
echo "The 'prize_id' element is in the array";
}
No output
无输出
In conclusion, array_key_exists()does not work with a simple array. Its only to find whether an array key exist or not. Use in_array()instead.
总之,array_key_exists()不适用于简单的数组。它只是查找数组键是否存在。使用in_array()来代替。
Here is more example:
这是更多示例:
<?php
/**++++++++++++++++++++++++++++++++++++++++++++++
* 1. example with assoc array using in_array
*
* IMPORTANT NOTE: in_array is case-sensitive
* in_array — Checks if a value exists in an array
*
* DOES NOT WORK FOR MULTI-DIMENSIONAL ARRAY
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('a' => 'bla', 'b' => 'omg');
if (in_array('omg', $something)) {
echo "|1| The 'omg' value found in the assoc array ||";
}
/**++++++++++++++++++++++++++++++++++++++++++++++
* 2. example with index array using in_array
*
* IMPORTANT NOTE: in_array is case-sensitive
* in_array — Checks if a value exists in an array
*
* DOES NOT WORK FOR MULTI-DIMENSIONAL ARRAY
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('bla', 'omg');
if (in_array('omg', $something)) {
echo "|2| The 'omg' value found in the index array ||";
}
/**++++++++++++++++++++++++++++++++++++++++++++++
* 3. trying with array_search
*
* array_search — Searches the array for a given value
* and returns the corresponding key if successful
*
* DOES NOT WORK FOR MULTI-DIMENSIONAL ARRAY
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('a' => 'bla', 'b' => 'omg');
if (array_search('bla', $something)) {
echo "|3| The 'bla' value found in the assoc array ||";
}
/**++++++++++++++++++++++++++++++++++++++++++++++
* 4. trying with isset (fastest ever)
*
* isset — Determine if a variable is set and
* is not NULL
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('a' => 'bla', 'b' => 'omg');
if($something['a']=='bla'){
echo "|4| Yeah!! 'bla' found in array ||";
}
/**
* OUTPUT:
* |1| The 'omg' element value found in the assoc array ||
* |2| The 'omg' element value found in the index array ||
* |3| The 'bla' element value found in the assoc array ||
* |4| Yeah!! 'bla' found in array ||
*/
?>
Here is PHP DEMO
这是 PHP DEMO
回答by Jasir
You can use:
您可以使用:
array_search()in_array()- a combination of
array_flip()andarray_key_exists()
array_search()in_array()- 的组合
array_flip()和array_key_exists()
回答by echo
To check if the index is defined: isset($something['say'])
要检查索引是否已定义: isset($something['say'])
回答by Tom Jowitt
in_array() is fine if you're only checking but if you need to check that a value exists and return the associated key, array_search is a better option.
in_array() 如果您只是检查就可以,但如果您需要检查值是否存在并返回关联的键,则 array_search 是更好的选择。
$data = [
'hello',
'world'
];
$key = array_search('world', $data);
if ($key) {
echo 'Key is ' . $key;
} else {
echo 'Key not found';
}
This will print "Key is 1"
这将打印“Key is 1”
回答by VolkerK
You can test whether an array has a certain element at all or not with isset()or sometimes even better array_key_exists()(the documentation explains the differences). If you can't be sure if the array has an element with the index 'say' you should test that first or you might get 'warning: undefined index....' messages.
您可以使用isset()或有时甚至更好的array_key_exists()测试数组是否具有某个元素(文档解释了差异)。如果您不能确定数组是否有一个索引为“say”的元素,您应该首先测试它,否则您可能会收到“警告:未定义索引....”消息。
As for the test whether the element's value is equal to a string you can use == or (again sometimes better) the identity operator ===which doesn't allow type juggling.
至于测试元素的值是否等于字符串,您可以使用 == 或(有时更好)身份运算符===不允许类型杂耍。
if( isset($something['say']) && 'bla'===$something['say'] ) {
// ...
}
回答by Vishnu Sharma
<?php
if (in_array('your_variable', $Your_array)) {
$redImg = 'true code here';
} else {
$redImg = 'false code here';
}
?>
回答by Xman Classical
Just use the PHP function array_key_exists()
只需使用PHP函数 array_key_exists()
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
回答by animuson
Well, first off an associative array can only have a key defined once, so this array would never exist. Otherwise, just use in_array()to determine if that specific array element is in an array of possible solutions.
好吧,首先关联数组只能定义一次键,所以这个数组永远不会存在。否则,仅用于in_array()确定该特定数组元素是否在可能解决方案的数组中。

