dqkx69935 2018-10-23 05:15 采纳率: 100%
浏览 215
已采纳

如何检查SimpleXMLElement对象的属性是否存在 - PHP

In the below code I'm trying to access @attributes of Userinfo as $somevariable->Userinfo->attributes(); which is perfectly fine, but when there is no @attributes it throws an error

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
        )

)

So, before accessing @attributes, how to check if it exist ?

Thank you.

  • 写回答

1条回答 默认 最新

  • dongxie2749 2018-10-23 05:17
    关注

    You can use !empty()

    if(!empty($somevariable->Userinfo->attributes())){
      // do stuff here
    }
    

    Sample output:- https://3v4l.org/R32Bv

    Or you can use isset() too

    if(isset($somevariable->Userinfo)){
      // do stuff here
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制