0

I have the code below:

SimpleXMLElement Object(
    [@attributes] => Array(
        [id] => 542
        [url] => http://google.pl
        [price] => 19.29
        [avail] => 1
        [set] => 0
    )
)

How can I get access to id with PHP?

Fabio
  • 23,183
  • 12
  • 55
  • 64
Marcin
  • 994
  • 2
  • 11
  • 26
  • Possible duplicate of [PHP get values from SimpleXMLElement array](http://stackoverflow.com/questions/2751711/php-get-values-from-simplexmlelement-array) – L. Herrera Dec 28 '16 at 11:02

2 Answers2

1

Try this

$attributes = $simpleXmlElement->attributes();
echo $id = $attributes['id'];
Andrii Mishchenko
  • 2,626
  • 21
  • 19
1
function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
}
print xml_attribute($xml, 'id'); //prints "542"

I can get the "id" like this

mickmackusa
  • 43,625
  • 12
  • 83
  • 136