php SimpleXML attributes are missing

The simple answer here is not to use print_r() with SimpleXML objects. Because they are wrappers around non-PHP data, functions like that which would normally show the “whole” object don’t really reflect what you’re looking at.

The way to access an attribute with SimpleXML is to use the attribute name as though it was an array key ($node['attribute']); this does not mean that there is an array somewhere with that key, it is a function-call in disguise.

If you want to get a feel for which nodes you’re looking at while writing SimpleXML code, check out this simplexml_dump() function which I wrote (feedback welcome).

Leave a Comment