php 如何检查关联数组是否具有空值或空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16469947/
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
How to check if an associative array has an empty or null value
提问by bikey77
In the following associative array
在以下关联数组中
$array = array(
[0] => 0
[1] => 1
[2] =>
[3] => 2
[4] =>
)
how can you determine if a given key has an empty (or null) value? I used
如何确定给定键是否具有空(或空)值?我用了
if(empty($array[$value]))
and
和
if(isset($array[$value])) && $array[$value] !=='')
When using empty
I also get false
for the first array value which is zero and isset
doesn't seem to do the trick.
使用时,empty
我也得到false
第一个数组值,该值为零并且 isset
似乎不起作用。
回答by hek2mgl
use array_key_exists()
and is_null()
for that. It will return TRUE
if the key exists and has a value far from NULL
使用array_key_exists()
和is_null()
为此。它将返回TRUE
如果该键存在,并且有一个价值远NULL
Difference:
区别:
$arr = array('a' => NULL);
var_dump(array_key_exists('a', $arr)); // --> TRUE
var_dump(isset($arr['a'])); // --> FALSE
So you should check:
所以你应该检查:
if(array_key_exists($key, $array) && is_null($array[$key])) {
echo "key exists with a value of NULL";
}
回答by klidifia
Looked at all the answers and I don't like them. Isn't this much simpler and better? It's what I am using:
看了所有的答案,我不喜欢它们。这不是更简单更好吗?这就是我正在使用的:
if (in_array(null, $array, true) || in_array('', $array, true)) {
// There are null (or empty) values.
}
Note that setting the third parameter as true means strict comparison, this means 0 will not equal null - however, neither will empty strings ('') - this is why we have two conditions. Unfortunately the first parameter in in_array has to be a string and cannot be an array of values.
请注意,将第三个参数设置为 true 意味着严格比较,这意味着 0 不等于 null - 然而,空字符串 ('') 也不 - 这就是我们有两个条件的原因。不幸的是 in_array 中的第一个参数必须是字符串,不能是值数组。
回答by Rolando Isidoro
PHP emptyreturn values states:
PHP空返回值状态:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
如果 var 存在且具有非空、非零值,则返回 FALSE。否则返回 TRUE。
以下内容被认为是空的:
""(空字符串)
0(0 作为整数)
0.0(0 作为浮点数)
“0”(0 作为字符串)
空值
错误的
array()(空数组)
$var; (声明的变量,但没有值)
From your array example I take it as you want to exclude the 0 as an integer. If that's the case this would do the trick:
从您的数组示例中,我认为您想将0 作为整数排除在外。如果是这种情况,这将起作用:
<?php
$array = array(0, 1, '', 2, '');
foreach ($array as $value) {
echo (empty($value) && 0 !== $value) ? "true\n" : "false\n";
}
If you want to exclude other conditions that empty
considers just negate them in that condition. Take in account that this might not be the optimal solution if you want to check other values.
如果你想排除其他条件,empty
只考虑在该条件下否定它们。如果您想检查其他值,请考虑到这可能不是最佳解决方案。
回答by Andrey Volk
if ( !isset($array[$key]) || $array[$key] == "" || is_null($array[$key]) )
{
//given key does not exist or it has "" or NULL value
}
回答by dougBTV
Potentially this could be cleaner if I knew how the array was constructed, but, having the assumption that you can have both empty strings, or nulls in the array, and you want to account for values of 0 --> here's what I'd do:
如果我知道数组是如何构造的,这可能会更清晰,但是,假设数组中可以有空字符串或空值,并且您想考虑 0 的值 --> 这就是我想要的做:
if (is_null($array[$key]) || (string)$array[$key] == '')
Here's a little bit of test code showing it in action with an array that has both 0, null, an empty string, and non-zero integers...
这是一些测试代码,显示了它与一个同时具有 0、null、空字符串和非零整数的数组的作用......
$array = array(0,1,null,2,'');
print_r($array);
foreach ($array as $key => $val) {
if (is_null($array[$key]) || (string)$array[$key] == '') {
echo $key.", true\n";
}
}
As for using isset() -- an empty string is consider to be set. Which may be what you're running into (aside from 0 being considered empty) Compare with this usage:
至于使用 isset() - 考虑设置一个空字符串。这可能是您遇到的(除了 0 被认为是空的)与此用法进行比较:
$foo = array(0,1,null,2,'');
print_r($foo);
foreach ($foo as $key => $val) {
if (isset($foo[$key])) {
echo $key.", true\n";
}
}
回答by jterry
foreach($array as $i => $v) {
if(null === $v) {
// this item ($array[$i]) is null
}
}
...or, for a given key:
...或者,对于给定的键:
if(null === $array[2]) {
// this item ($array[2]) is null
}