0

I have a RSS object $rssObject created using the PHP simplexml_load_file function, the goal is to get the value of [href].

var_dump($rssObject) returns the following:

Array
(
    [0] => SimpleXMLElement Object
        (
            [link] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [href] => https://www.example.com
                                )

                        )

I have tried unsuccessfully to get the contents of [href] using this notation which returns null

$rssObject[0]->link[0]->{'@attributes'}['href'];

Why is this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mohammad
  • 7,344
  • 15
  • 48
  • 76
  • Why has this been down-voted? Please see answer, it justified this question when the var_dump is outputting a misleading output! – Mohammad Jul 14 '17 at 10:01
  • 1
    Yeah, this is a very common confusion, and we should really have a canonical answer explaining it. – IMSoP Jul 14 '17 at 11:36

1 Answers1

2

In SimpleXML, attributes are accessed using array notation:

$xml = simplexml_load_file();
$url = $xml[0]->link[0]['href'];

See "Example #5 Using attributes" in the PHP manual.

cweiske
  • 30,033
  • 14
  • 133
  • 194