This is the output of print_r()
run on a typical SimpleXMLElement object:
SimpleXMLElement Object
(
[@attributes] => Array
(
)
)
What does the @ sign mean?
This is the output of print_r()
run on a typical SimpleXMLElement object:
SimpleXMLElement Object
(
[@attributes] => Array
(
)
)
What does the @ sign mean?
All those answers about error control are incorrect. The @ doesn't mean anything. That's how the property is called internally, but do not rely on this. Do not rely on
print_r()
orvar_dump()
when dealing with SimpleXML. SimpleXML does a lot of "magical" things that are not correctly represented byprint_r()
andvar_dump()
.If you need to know what's "inside" a XML fragment, just use
->asXML()
on it.Sorry, can't comment as a guest but for anyone else who ends up here like I did... I am creating my own Joomla form fields and Joomla creates a very 'interesting' object of all sorts of things. Now, I didn't want to become a SimpleXML expert, all I wanted was the original label text which was squirrelled away in @attributes.
After a bit of
"hmmm, I wonder if this works?"™
I found this is the easiest way of accessing these values:They were not lying. It really is simple.
I am working with an HTTP API that gives out only XML formatted data. So first I loaded it into SimpleXML and was also puzzled by the @attributes issue.. how do I get at the precious data it contains? print_r() confused me.
My solution was to create an array and an iterator variable at 0. Loop through a SimpleXML object with foreach and get at the data with the attribues() method and load it into my created array. Iterate before foreach loop ends.
So print_r() went from showing this:
To a much more usable normal array. Which is great because I wanted the option to quickly convert array into json if needed.
My solution in code:
Quick note would be PHP's settype() function is weird/buggy, so I added the + to make sure ID is an integer and added the quotes to make sure the name is string. If there isn't a variable conversion of some kind, you're going to be loading SimpleXML objects into the array you created.
Final result of print_r():
I don't have enough reps to comment on user3098738... but wanted to validate his response. It really is that simple. Any time you run in to @attributes in SimpleXML... use
This is a SimpleXMLElement object. The '@attributes' row is an internal representation of the attributes from the XML element. Use SimpleXML's functions to get data from this object rather than interacting with it directly.