3

I'm using YouTube API. I got following result from API :

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [rel] => alternate
            [href] => http://www.youtube.com/watch?v=blabla
        )

)

I'm confuse with this object. I want to access @attributes. How can i do it?

8
  • var_dump($xml->{'@attributes'}); $xml is your variable where u save xml Commented Jan 28, 2016 at 7:55
  • 1
    can't you just google this? php simplexml get attributes Commented Jan 28, 2016 at 7:55
  • @Ghost i got this $xml->attributes()->href. Thanks Commented Jan 28, 2016 at 8:00
  • @Mr.Engineer: and what about this $xml->{'@attributes'}->href ? Commented Jan 28, 2016 at 8:01
  • It should be something like this $xml->attributes['href'] because attributes is an array Commented Jan 28, 2016 at 8:02

1 Answer 1

2

The @attributes part of the print_r output are just the element's attributes which can be accessed via $obj['attrname'].

<?php
$obj = new SimpleXMLElement('<foo rel="alternate" href="http://www.youtube.com/watch?v=blabla" />');
print_r($obj); // to verify that the sample data fits your actual data

echo $obj['rel'], ' | ', $obj['href'];

prints

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [rel] => alternate
            [href] => http://www.youtube.com/watch?v=blabla
        )

)
alternate | http://www.youtube.com/watch?v=blabla

see also Example #5 Using attributes in the SimpleXML documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.