如何检查数组值是否存在?

如何检查$something['say']是否具有'bla''omg'

 $something = array('say' => 'bla', 'say' => 'omg'); 

使用if

 if(isset($something['say']) && $something['say'] == 'bla') { // do something } 

顺便说一句,你是分配一个值与键say两次,因此你的数组将导致只有一个值的数组。

你可以使用PHP in_array函数

 if( in_array( "bla" ,$yourarray ) ) { echo "has bla"; } 

使用: 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"; } 

这里是输出: The 'prize_id' element is in the array


使用: 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"; } 

没有输出


总之, array_key_exists()不适用于一个简单的数组。 它只能find一个数组键是否存在。 使用in_array()代替。

这里是更多的例子:

 <?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 || */ ?> 

这里是PHP DEMO

检查索引是否被定义: isset($something['say'])

您可以使用:

  • array_search()
  • in_array()
  • array_flip()array_key_exists()

你可以testing一个数组是否有一个特定的元素,或不使用isset() ,有时甚至更好array_key_exists() (文档解释了差异)。 如果你不能确定数组是否有一个索引为'say'的元素,你应该先testing一下,否则你可能会得到'warning:undefined index ….'消息。

至于testing元素的值是否等于一个string,您可以使用==或(有时候更好)身份运算符===这不允许types杂耍 。

 if( isset($something['say']) && 'bla'===$something['say'] ) { // ... } 

in_array()是好的,如果你只是检查,但如果你需要检查一个值是否存在并返回相关的键,array_search是一个更好的select。

 $data = array( 0 => 'hello', 1 => 'world' ); $key = array_search('world', $data); if ($key) { echo 'Key is ' . $key; } else { echo 'Key not found'; } 

这将打印“密钥是1”

只需使用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"; } ?> 
 <?php if (in_array('your_variable', $Your_array)) { $redImg = 'true code here'; } else { $redImg = 'false code here'; } ?> 

那么,首先关联数组只能有一个键定义一次,所以这个数组永远不会存在。 否则,只需使用in_array()来确定特定数组元素是否在可能解决scheme的数组中。

 bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) 

另一种使用in_array in_array()作为针的数组

 <?php $a = array(array('p', 'h'), array('p', 'r'), 'o'); if (in_array(array('p', 'h'), $a)) { echo "'ph' was found\n"; } if (in_array(array('f', 'i'), $a)) { echo "'fi' was found\n"; } if (in_array('o', $a)) { echo "'o' was found\n"; } ?> 

假设你正在使用一个简单的数组

。 即

 $MyArray = array("red","blue","green"); 

你可以使用这个function

 function val_in_arr($val,$arr){ foreach($arr as $arr_val){ if($arr_val == $val){ return true; } } return false; } 

用法:

 val_in_arr("red",$MyArray); //returns true val_in_arr("brown",$MyArray); //returns false