I’m using SimpleXML to get some data from an API. Its returning things in this format:
object(SimpleXMLElement)#10 (1) {
[0]=>
string(36) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
My question is, how can I possibly access the string value of this object? If I try to do $myVariable->0 that gives me an error. Doing $zero = '0' and then echo $myVariable->$zero doesn’t work either, nor does (array) $myVariable work (that gives a warning).
The trick is that
SimpleXMLElementhas__toStringmagic method implemented that would return yourstring(36) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", so to get this string you just cast (string) on yourSimpleXMLElementobject:With
PHPyou canof course, so explicit
(string)here is not necessarily needed.