php 检查数组是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8328983/
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
Check whether an array is empty
提问by gbhall
I have the following code
我有以下代码
<?php
$error = array();
$error['something'] = false;
$error['somethingelse'] = false;
if (!empty($error))
{
echo 'Error';
}
else
{
echo 'No errors';
}
?>
However, empty($error)
still returns true
, even though nothing is set.
但是,即使没有设置,empty($error)
仍然返回true
。
What's not right?
什么不对?
回答by ioseb
There are two elements in array and this definitely doesn't mean that array is empty. As a quick workaround you can do following:
数组中有两个元素,这绝对不意味着数组为空。作为快速解决方法,您可以执行以下操作:
$errors = array_filter($errors);
if (!empty($errors)) {
}
array_filter()
function's default behavior will remove all values from array which are equal to null
, 0
, ''
or false
.
array_filter()
功能的默认行为将删除阵列等于所有值null
,0
,''
或false
。
Otherwise in your particular case empty()
construct will always return true
if there is at least one element even with "empty" value.
否则,在您的特定情况下,如果至少有一个元素具有“空”值,则empty()
构造将始终返回true
。
回答by brenjt
You can also check it by doing.
您也可以通过执行来检查它。
if(count($array) > 0)
{
echo 'Error';
}
else
{
echo 'No Error';
}
回答by 0xd
Try to check it's size with sizeof
if 0
no elements.
sizeof
如果0
没有元素,请尝试检查它的大小。
回答by wrren
PHP's built-in empty()function checks to see whether the variable is empty, null, false, or a representation of zero. It doesn't return true just because the value associated with an array entry is false, in this case the array has actual elements in it and that's all that's evaluated.
PHP 的内置empty()函数会检查变量是否为空、null、false 或零的表示。它不会仅仅因为与数组条目关联的值是 false 就返回 true,在这种情况下,数组中有实际元素,这就是所有评估的内容。
If you'd like to check whether a particular error condition is set to truein an associative array, you can use the array_keys()function to filter the keys that have their value set to true.
如果要检查关联数组中的特定错误条件是否设置为true,可以使用array_keys()函数过滤值设置为 true 的键。
$set_errors = array_keys( $errors, true );
You can then use the empty() function to check whether this array is empty, simultaneously telling you whether there are errors and also which errors have occurred.
然后你可以使用 empty() 函数来检查这个数组是否为空,同时告诉你是否有错误以及发生了哪些错误。
回答by Eelco
array with zero elements converts to false
具有零元素的数组转换为 false
回答by Pekka
回答by David
From the PHP-documentation:
来自 PHP 文档:
Returns FALSE if var has a non-empty and non-zero value.
如果 var 具有非空和非零值,则返回 FALSE。
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 $var; (a variable declared, but without a value in a class)
回答by thewebsurfer
function empty()
does not work for testing empty arrays!
example:
函数empty()
不适用于测试空数组!例子:
$a=array("","");
if(empty($a)) echo "empty";
else echo "not empty"; //this case is true
a function is necessary:
一个函数是必要的:
function is_array_empty($a){
foreach($a as $elm)
if(!empty($elm)) return false;
return true;
}
ok, this is a very old question :) , but i found this thread searching for a solution and i didnt find a good one.
好的,这是一个非常古老的问题:),但我发现这个线程正在寻找解决方案,但我没有找到一个好的解决方案。
bye (sorry for my english)
再见(对不起我的英语)
回答by Pulkit Ambliya
hi array is one object so it null type or blank
嗨数组是一个对象,所以它是空类型或空白
<?php
if($error!=null)
echo "array is blank or null or not array";
//OR
if(!empty($error))
echo "array is blank or null or not array";
//OR
if(is_array($error))
echo "array is blank or null or not array";
?>
回答by Reza Baradaran Gazorisangi
In PHP, even if the individual items within an array or properties of an object are empty, the array or object will not evaluate to empty using the empty($subject)
function. In other words, cobbling together a bunch of data that individually tests as "empty" creates a composite that is non-empty.
Use the following PHP function to determine if the items in an array or properties of an object are empty:
在 PHP 中,即使数组中的单个项或对象的属性为空,数组或对象也不会使用该empty($subject)
函数计算为空。换句话说,将一堆单独测试为“空”的数据拼凑在一起会创建一个非空的组合。使用以下 PHP 函数确定数组中的项或对象的属性是否为空:
function functionallyEmpty($o)
{
if (empty($o)) return true;
else if (is_numeric($o)) return false;
else if (is_string($o)) return !strlen(trim($o));
else if (is_object($o)) return functionallyEmpty((array)$o);
// If it's an array!
foreach($o as $element)
if (functionallyEmpty($element)) continue;
else return false;
// all good.
return true;
}
Example Usage:
示例用法:
$subject = array('', '', '');
empty($subject); // returns false
functionallyEmpty($subject); // returns true
class $Subject {
a => '',
b => array()
}
$theSubject = new Subject();
empty($theSubject); // returns false
functionallyEmpty($theSubject); // returns true