检查特定数组元素是否包含白名单值

问题描述 投票:0回答:12

如何检查

$something['say']
是否具有
'bla'
'omg'
的值?

$something = array('say' => 'bla', 'say' => 'omg');
php arrays associative-array
12个回答
367
投票

您可以使用 PHP in_array 函数:

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

136
投票

使用说明

if

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

顺便说一句,您使用键

say
分配一个值两次,因此您的数组将生成一个只有一个值的数组。


65
投票

使用:

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()
不适用于简单数组。只是判断一个数组的key是否存在。请使用
in_array()
来代替。

以下是更多示例:

<?php
/**++++++++++++++++++++++++++++++++++++++++++++++
 * 1. Example with an associative 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 associative array ||";
}

/**++++++++++++++++++++++++++++++++++++++++++++++
 * 2. Example with an 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 A 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 演示


11
投票

您可以使用:


9
投票
如果您只是进行检查,则

in_array() 没问题,但如果您需要检查值是否存在并返回关联的键,则 array_search 是更好的选择。

$data = [
    'hello',
    'world'
];

$key = array_search('world', $data);

if ($key) {
    echo 'Key is ' . $key;
} else {
    echo 'Key not found';
}

这将打印“Key is 1”。


5
投票

检查索引是否已定义:

isset($something['say'])


5
投票

您可以使用 isset() 测试数组是否具有某个元素,有时甚至更好 array_key_exists() (文档解释了差异)。如果您无法确定数组中是否有一个索引为“say”的元素,您应该首先测试它,否则您可能会收到“警告:未定义的索引...”消息。

至于测试元素的值是否等于字符串,您可以使用 == 或(有时更好)身份运算符 === ,它不允许 类型杂耍

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

3
投票

只需使用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";
}
?>

2
投票
<?php
if (in_array('your_variable', $Your_array)) {
    $redImg = 'true code here';
} else {
    $redImg = 'false code here';
} 
?>

1
投票
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";
}
?>

1
投票

假设您使用的是简单数组,即

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

您可以使用此功能

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

0
投票

关联数组只能定义一次键,因此该数组永远不会存在。否则,只需使用

in_array()
来确定该特定数组元素是否在可能解决方案的数组中。

© www.soinside.com 2019 - 2024. All rights reserved.