本文介绍了检查是否设置了对象属性 - 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更多推荐
检查是否设置了对象属性
发布评论