Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Something special about ['@attributes'] in simplexml load string()?

1:05 pm on Oct 15, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1205
votes: 120

I fetch an external XML feed, then encode it to JSON, then decode it again using:

$xml = simplexml_load_string($contents);
$json = json_encode($xml);
$rss = json_decode($json, TRUE);

So then I have elements like $rss['channel']['item'][0]['link'].

Here's what's weird. I look at the ['link'] array using:


and I have a single line, which is the website address provided by the feed. Perfect, great, wonderful.

The Google Alerts feed uses a weird format, , though, and puts the address at $rss['channel']['item'][0]['link']['@attributes']['href'] (that shows up under print_r()). So I double check to see if that key exists with:

if ($rss['channel']['item'][0]['link']['@attributes']['href'])
// use this link instead

But even though print_r() shows that there are no other keys or values beneath ['link'] in this particular feed, for some reason it's still showing a value of h.

(And I checked using gettype()... it's a string, not an array of every character in the link).

Since I don't have a defined element of ['href'] in the array, I'm at a loss on why this is showing up. Is ['@attributes']['href'] a special defined value that's just not going to show up with print_r()?

If so, how do I check if the literal $rss['channel']['item'][0]['link']['@attributes']['href'] element exists?