Marcin
Marcin

Reputation: 1024

Access to SimpleXMLElement Object value

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?

Upvotes: 0

Views: 217

Answers (2)

Sankar Ganesh
Sankar Ganesh

Reputation: 61

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

Upvotes: 1

Andrii Mishchenko
Andrii Mishchenko

Reputation: 2694

Try this

$attributes = $simpleXmlElement->attributes();
echo $id = $attributes['id'];

Upvotes: 1

Related Questions