检查是否设置了对象属性 - SimpleXML [英] Checking if an object attribute is set - SimpleXML

查看:72
本文介绍了检查是否设置了对象属性 - SimpleXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 XML 我正在使用 PHP 的 SimpleXML 类,并且我在 XML 中有一些元素,例如:

I have some XML I am using PHP's SimpleXML class with and I have elements within the XML such as:

<condition id="1" name="New"></condition>
<condition id="2" name="Used"></condition>

但是它们并不总是存在,所以我需要先检查它们是否存在.

However they are not always there, so I need to check if they exist first.

我试过了..

if (is_object($bookInfo->page->offers->condition['used'])) {
    echo 'yes';
}

还有..

if (isset($bookInfo->page->offers->condition['used'])) {
    echo 'yes';
}

但两者都不起作用.它们仅在我删除属性部分时才起作用.

But neither work. They only work if I remove the attribute part.

那么如何检查属性是否设置为对象的一部分?

So how can I check to see if an attribute is set as part of an object?

推荐答案

您正在查看的是属性值.您需要查看属性(在本例中为 name)本身:

What you're looking at is the attribute value. You need to look at the attribute (name in this case) itself:

if (isset($bookInfo->page->offers->condition['name']) && $bookInfo->page->offers->condition['name'] == 'Used')
    //-- the rest is up to you

这篇关于检查是否设置了对象属性 - SimpleXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆