如何在PHP中查找对象是否为空.
以下是$obj保存XML数据的代码.如何检查它是否为空?
我的代码:
$obj = simplexml_load_file($url);
Run Code Online (Sandbox Code Playgroud)
Pet*_*lly 117
您可以强制转换为数组,然后检查它是否为空
$arr = (array)$obj;
if (empty($arr)) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
编辑:单行类型转换,如下所示,在PHP 5.4中不起作用:
if (empty((array) $obj)) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*orn 29
编辑:我没有意识到他们想要专门检查SimpleXMLElement对象是否为空.我在下面留下了旧答案
对于SimpleXMLElement:
如果为空则表示没有属性:
$obj = simplexml_load_file($url);
if ( !$obj->count() )
{
// no properties
}
Run Code Online (Sandbox Code Playgroud)
要么
$obj = simplexml_load_file($url);
if ( !(array)$obj )
{
// empty array
}
Run Code Online (Sandbox Code Playgroud)
如果SimpleXMLElement是一个深度级别,并且为空,则实际上意味着它只有PHP认为falsey(或没有属性)的属性:
$obj = simplexml_load_file($url);
if ( !array_filter((array)$obj) )
{
// all properties falsey or no properties at all
}
Run Code Online (Sandbox Code Playgroud)
如果SimpleXMLElement的深度超过一级,则可以将其转换为纯数组:
$obj = simplexml_load_file($url);
// `json_decode(json_encode($obj), TRUE)` can be slow because
// you're converting to and from a JSON string.
// I don't know another simple way to do a deep conversion from object to array
$array = json_decode(json_encode($obj), TRUE);
if ( !array_filter($array) )
{
// empty or all properties falsey
}
Run Code Online (Sandbox Code Playgroud)
如果要检查简单对象(类型stdClass)是否完全为空(没有键/值),可以执行以下操作:
// $obj is type stdClass and we want to check if it's empty
if ( $obj == new stdClass() )
{
echo "Object is empty"; // JSON: {}
}
else
{
echo "Object has properties";
}
Run Code Online (Sandbox Code Playgroud)
资料来源:http://php.net/manual/en/language.oop5.object-comparison.php
编辑:添加示例
$one = new stdClass();
$two = (object)array();
var_dump($one == new stdClass()); // TRUE
var_dump($two == new stdClass()); // TRUE
var_dump($one == $two); // TRUE
$two->test = TRUE;
var_dump($two == new stdClass()); // FALSE
var_dump($one == $two); // FALSE
$two->test = FALSE;
var_dump($one == $two); // FALSE
$two->test = NULL;
var_dump($one == $two); // FALSE
$two->test = TRUE;
$one->test = TRUE;
var_dump($one == $two); // TRUE
unset($one->test, $two->test);
var_dump($one == $two); // TRUE
Run Code Online (Sandbox Code Playgroud)
Moh*_*rbi 18
您可以将对象转换为数组,并测试其计数如下:
if(count((array)$obj)) {
// doStuff
}
Run Code Online (Sandbox Code Playgroud)
想象一下,如果对象不是空的并且方式非常大,为什么要浪费资源将其转换为数组或序列化...
这是我在JavaScript中使用的一个非常简单的解决方案.与上面提到的将对象转换为数组并检查它是否为空的解决方案或将其编码为JSON的解决方案不同,这个简单的函数在使用资源执行简单任务时非常有效.
function emptyObj( $obj ) {
foreach ( $obj AS $prop ) {
return FALSE;
}
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
该解决方案以一种非常简单的方式工作:如果对象为空并且它将返回,它根本不会进入foreach循环true.如果它不是空的,它将进入foreach循环并立即返回false,而不是通过整个集合.
empty()在对象上使用时,使用将不会像往常一样工作,因为__isset()如果声明,将调用重载方法.
因此您可以使用count()(如果对象是可数的).
或者通过使用get_object_vars(),例如
get_object_vars($obj) ? TRUE : FALSE;
Run Code Online (Sandbox Code Playgroud)
另一种可能不需要铸造的解决方案array:
// test setup
class X { private $p = 1; } // private fields only => empty
$obj = new X;
// $obj->x = 1;
// test wrapped into a function
function object_empty( $obj ){
foreach( $obj as $x ) return false;
return true;
}
// inline test
$object_empty = true;
foreach( $obj as $object_empty ){ // value ignored ...
$object_empty = false; // ... because we set it false
break;
}
// test
var_dump( $object_empty, object_empty( $obj ) );
Run Code Online (Sandbox Code Playgroud)