PHP:检查数组键是否具有值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35083551/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:20:24  来源:igfitidea点击:

PHP: Check if array key has value

phparrays

提问by Liam Fell

I have the following array($post_options), it can appear in various guises, the key is dynamic:

我有以下数组($post_options),它可以以各种形式出现,关键是动态的:

It might be like this:

它可能是这样的:

array(1) { [95]=> string(2) "25" }

数组(1) { [95]=> 字符串(2) "25" }

It might be like this, the key can be anything:

可能是这样的,key可以是任何东西:

array(1) { [05]=> string(2) "" }

数组(1) { [05]=> 字符串(2) "" }

I want to add a control statement that controls upon whether or not the value in the first key element has a value.

我想添加一个控制语句来控制第一个键元素中的值是否具有值。

I have this code:

我有这个代码:

if (!isset($post_options[0])) {
            // value is empty
            var_dump($post_options);
        }
        else {
            // value has value
            echo 'Option Selected';
        }

But this is not working, it returns true when the value is set and not set. What is the solution here? Thanks

但这不起作用,它在设置和未设置值时返回 true。这里的解决方案是什么?谢谢

So if the array appears like this (the value of the first key is empty):

所以如果数组看起来像这样(第一个键的值为空):

array(1) { [05]=> string(2) "" }

数组(1) { [05]=> 字符串(2) "" }

I want to var_dump();in this case

我想var_dump();在这种情况下

采纳答案by u_mulder

If you don't know the first key of array - use array_keys, for example, to get all keys of your array.

如果您不知道数组的第一个键 -array_keys例如,使用 获取数组的所有键。

$ar = array('95' => 'val');
$keys = array_keys($ar);
$first_item = $ar[$keys[0]]; 
var_dump($first_item);    // outputs: string(3) "val" 

Another option can be a currentfunction, which will return you current element of an array. Considering that you don't move array pointer, code can be:

另一个选项可以是一个current函数,它将返回数组的当前元素。考虑到你不移动数组指针,代码可以是:

$ar = array('95' => 'new_val', '02' => 'val2');
$cur = current($ar);
var_dump($cur);  // outputs: string(7) "new_val" 

After that you can use standard emptyfunction to check:

之后,您可以使用标准empty功能来检查:

if (empty($cur))

or

或者

if (empty($first_item))

回答by Ken Wayne VanderLinde

Depending on your exact requirements, there are three alternatives.

根据您的具体要求,有三种选择。

  1. array_key_exists($key, $array)This is the simplest option. It returns trueif the array has the given key, and otherwise returns false.

  2. isset($array[$key])Slightly more strict than array_key_existssince it also requires that the value assigned to the key is not null.

  3. !empty($array[$key])(note the !) This is the strictest option, as it requires the value to not be any empty value (i.e., not null, false, 0, '0', etc).

  1. array_key_exists($key, $array)这是最简单的选择。它返回true,如果数组有给定的密钥,否则返回false

  2. isset($array[$key])array_key_exists它更严格,因为它还要求分配给键的值不是null

  3. !empty($array[$key])(注意!)这是最严格的选择,因为它需要的值没有任何空值(即,不nullfalse0'0',等)。

There's some other options, again, depending on exact requirements. It looks to me that you are looking for the !emptyoption, since it will (in particular) reject empty strings.

还有一些其他的选择,同样,取决于确切的要求。在我看来,您正在寻找该!empty选项,因为它会(特别是)拒绝空字符串。

回答by Rockers Technology

try this

尝试这个

if (!isset($post_options[0])) {            // value is empty
    if(!empty($post_options[0])) {
          var_dump($post_options);          
    }           
}

回答by ponciste

isset checks whether the array position exists or not, not whether it cointains or it doesn't contain a value.

isset 检查数组位置是否存在,而不是它是否包含或不包含值。

You better have a look at the emptyfunction to do this

你最好看看函数来做到这一点

if (empty($post_options[0])) {
    // value is empty
    var_dump($post_options);
} else {
    // value has value
    echo 'Option Selected';
}

hope this helps

希望这可以帮助