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
  • 22,442
  • 12
  • 51
  • 63
Marcin
  • 956
  • 2
  • 10
  • 24
  • 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,588
  • 20
  • 18
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
  • 37,596
  • 11
  • 75
  • 105