如何解决从SimpleXML对象中提取属性
| 如何从此xml对象提取属性<designs>
<tags>
.
.
.
</tags>
<templates>
<template id=\"photographysite\" image=\"http://example.com/en/previews/photographysitePreview434x326.jpg\" name=\"Shutter\" thumb=\"http://example.com/en/previews/photographysitePreview182x137.jpg\">
<tag>all</tag>
<tag>featured</tag>
<tag>personal</tag>
<tag>portfolio</tag>
<tag>photography</tag>
<tag>business</tag>
</template>
</templates>
</designs>
如果我将每个对象都视为$template
,则此语法将不起作用。
foreach ($xmldoc->templates as $template) {
$attributes = $template->attributes();
echo \'<img src=\"\' . $attributes[\'thumb\'] . \'\" />\';
}
解决方法
您可以使用数组符号访问单个属性,例如
foreach ($xmldoc->templates->template as $template) {
echo \'<img src=\"\',$template[\'thumb\'],\'\"/>\';
}
参见http://www.php.net/manual/en/simplexml.examples-basic.php#example-4587
, foreach($template->foo[0]->attributes() as $a => $b):
http://php.net/manual/zh/simplexmlelement.attributes.php
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。