如何检查 SimpleXMLElement 对象的属性是否存在 - PHP
How to check if SimpleXMLElement Object's attribute exist - PHP
在下面的代码中,我试图以 $somevariable->Userinfo->attributes();
的形式访问 Userinfo
的 @attributes
,这完全没问题,但是当没有 @attributes
时,它会抛出一个错误
Call to a member function attributes() on null
[Userinfo] => SimpleXMLElement Object
(
[@attributes] => Array
(
[UserId] => 121
[UserName] => Arun, Singh
[UserEmail] => abc@xyz.com
[CreatedDate] => 06/27/2018 08:44:21
)
)
那么,在访问@attributes
之前,如何检查它是否存在?
谢谢。
您可以使用!empty()
if(!empty($somevariable->Userinfo->attributes())){
// do stuff here
}
示例输出:- https://3v4l.org/R32Bv
或者你也可以使用 isset()
if(isset($somevariable->Userinfo)){
// do stuff here
}
在下面的代码中,我试图以 $somevariable->Userinfo->attributes();
的形式访问 Userinfo
的 @attributes
,这完全没问题,但是当没有 @attributes
时,它会抛出一个错误
Call to a member function attributes() on null
[Userinfo] => SimpleXMLElement Object
(
[@attributes] => Array
(
[UserId] => 121
[UserName] => Arun, Singh
[UserEmail] => abc@xyz.com
[CreatedDate] => 06/27/2018 08:44:21
)
)
那么,在访问@attributes
之前,如何检查它是否存在?
谢谢。
您可以使用!empty()
if(!empty($somevariable->Userinfo->attributes())){
// do stuff here
}
示例输出:- https://3v4l.org/R32Bv
或者你也可以使用 isset()
if(isset($somevariable->Userinfo)){
// do stuff here
}