检查是否设置了对象属性

编程入门 行业动态 更新时间:2024-10-24 20:16:40
本文介绍了检查是否设置了对象属性 - 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

更多推荐

检查是否设置了对象属性

本文发布于:2023-11-27 10:26:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!