Answers:
使用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()
不适用于简单数组。唯一的发现数组键是否存在。使用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
array_key_exists()
检查数组键,而数组键$search_array
包含关联数组。毫无疑问,它不会起作用。你应该array_flip()
先。
您可以使用:
array_search()
in_array()
array_flip()
和array_key_exists()
您可以使用isset()或有时甚至更好的array_key_exists()来测试数组是否完全具有某个元素(文档解释了区别)。如果不确定数组是否具有索引为“ say”的元素,则应首先进行测试,否则可能会收到“警告:未定义的索引...”消息。
至于测试元素的值是否等于字符串,可以使用==,或者(有时更好)使用标识运算符===,它不允许类型变乱。
if( isset($something['say']) && 'bla'===$something['say'] ) {
// ...
}
<?php
if (in_array('your_variable', $Your_array)) {
$redImg = 'true code here';
} else {
$redImg = 'false code here';
}
?>
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";
}
?>