How do I extract attributes from this xml object
<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>
If I consider each object as $template
, then this syntax wont work.
foreach ($xmldoc->templates as $template) {
$attributes = $template->attributes();
echo '<img src="' . $attributes['thumb'] . '" />';
}
You can access individual attributes using the array notation, e.g.
foreach ($xmldoc->templates->template as $template) {
echo '<img src="', $template['thumb'], '"/>';
}
See http://www.php.net/manual/en/simplexml.examples-basic.php#example-4587
foreach($template->foo[0]->attributes() as $a => $b):