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

Uff*_*ffo 98 php arrays

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

$something = array('say' => 'bla', 'say' => 'omg');
Run Code Online (Sandbox Code Playgroud)

Ben*_*zar 267

您可以使用PHP in_array函数

if( in_array( "bla" ,$yourarray ) )
{
    echo "has bla";
}
Run Code Online (Sandbox Code Playgroud)

  • 是否可以使用相同键的阵列?第二个值不会覆盖原始值吗? (7认同)

Tat*_*nen 98

if

if(isset($something['say']) && $something['say'] == 'bla') {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你用键分配一个值say两次,因此你的数组将产生一个只有一个值的数组.


Nee*_*ngh 40

使用: 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";
}
Run Code Online (Sandbox Code Playgroud)

这是输出: 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";
}
Run Code Online (Sandbox Code Playgroud)

没有输出


总之,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 ||
 */
?>
Run Code Online (Sandbox Code Playgroud)

这是 PHP DEMO


Jas*_*sir 7

您可以使用:


ech*_*cho 6

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


Vol*_*erK 5

您可以使用isset()或有时甚至更好的array_key_exists()来测试数组是否具有某个元素(文档解释了这些差异).如果你不能确定数组是否有一个索引为'say'的元素,你应该首先测试它,否则你可能得到'warning:undefined index ....'消息.

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

if( isset($something['say']) && 'bla'===$something['say'] ) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)


Tom*_*itt 5

如果仅检查in_array()很好,但是如果需要检查值是否存在并返回关联的键,则array_search是一个更好的选择。

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

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

if ($key) {
    echo 'Key is ' . $key;
} else {
    echo 'Key not found';
}
Run Code Online (Sandbox Code Playgroud)

这将打印“ Key is 1”