This question already has an answer here:
这个问题已经有了答案:
- How to check whether an array is empty using PHP? 17 answers
- 如何使用PHP检查数组是否为空?17个答案
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?
不正确的是什么?
12 个解决方案
#1
380
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.
否则,在您的特殊情况下,空()构造将总是返回true,如果至少有一个元素是“空”值。
#2
63
You can also check it by doing.
你也可以通过做来检查。
if(count($array) > 0)
{
echo 'Error';
}
else
{
echo 'No Error';
}
#3
16
Try to check it's size with sizeof
if 0
no elements.
试着用sizeof if 0检查它的大小。
#4
13
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或0的表示形式。它不会返回true仅仅因为与数组条目相关联的值是false,在这种情况下,数组中有实际的元素,这就是所计算的全部。
If you'd like to check whether a particular error condition is set to true in 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()函数检查这个数组是否为空,同时告诉您是否有错误,以及发生了哪些错误。
#5
5
array with zero elements converts to false
元素为0的数组转换为false
http://php.net/manual/en/language.types.boolean.php
http://php.net/manual/en/language.types.boolean.php
#6
4
However, empty($error) still returns true, even though nothing is set.
但是,即使没有设置任何内容,empty($error)仍然返回true。
That's not how empty()
works. According to the manual, it will return true on an empty array only. Anything else wouldn't make sense.
empty()不是这样工作的。根据手册,它只在空数组上返回true。其他的都说不通。
#7
2
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)
#8
2
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)
再见(对不起我的英语)
#9
1
<?php
if(empty($myarray))
echo"true";
else
echo "false";
?>
#10
1
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
#11
1
hi array is one object so it null type or blank
hi数组是一个对象,所以它是空类型或空类型
<?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";
?>
#12
0
I can't replicate that (php 5.3.6):
我无法复制(php 5.3.6):
php > $error = array();
php > $error['something'] = false;
php > $error['somethingelse'] = false;
php > var_dump(empty($error));
bool(false)
php > $error = array();
php > var_dump(empty($error));
bool(true)
php >
exactly where are you doing the empty()
call that returns true?
您在什么地方执行返回true的empty()调用?
#1
380
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.
否则,在您的特殊情况下,空()构造将总是返回true,如果至少有一个元素是“空”值。
#2
63
You can also check it by doing.
你也可以通过做来检查。
if(count($array) > 0)
{
echo 'Error';
}
else
{
echo 'No Error';
}
#3
16
Try to check it's size with sizeof
if 0
no elements.
试着用sizeof if 0检查它的大小。
#4
13
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或0的表示形式。它不会返回true仅仅因为与数组条目相关联的值是false,在这种情况下,数组中有实际的元素,这就是所计算的全部。
If you'd like to check whether a particular error condition is set to true in 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()函数检查这个数组是否为空,同时告诉您是否有错误,以及发生了哪些错误。
#5
5
array with zero elements converts to false
元素为0的数组转换为false
http://php.net/manual/en/language.types.boolean.php
http://php.net/manual/en/language.types.boolean.php
#6
4
However, empty($error) still returns true, even though nothing is set.
但是,即使没有设置任何内容,empty($error)仍然返回true。
That's not how empty()
works. According to the manual, it will return true on an empty array only. Anything else wouldn't make sense.
empty()不是这样工作的。根据手册,它只在空数组上返回true。其他的都说不通。
#7
2
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)
#8
2
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)
再见(对不起我的英语)
#9
1
<?php
if(empty($myarray))
echo"true";
else
echo "false";
?>
#10
1
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
#11
1
hi array is one object so it null type or blank
hi数组是一个对象,所以它是空类型或空类型
<?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";
?>
#12
0
I can't replicate that (php 5.3.6):
我无法复制(php 5.3.6):
php > $error = array();
php > $error['something'] = false;
php > $error['somethingelse'] = false;
php > var_dump(empty($error));
bool(false)
php > $error = array();
php > var_dump(empty($error));
bool(true)
php >
exactly where are you doing the empty()
call that returns true?
您在什么地方执行返回true的empty()调用?