Recursive cast from SimpleXMLObject to Array

Glen Solsberry picture Glen Solsberry · May 7, 2009 · Viewed 9.6k times · Source

I need to recursively cast a PHP SimpleXMLObject to an array. The problem is that each sub element is also a PHP SimpleXMLElement.

Is this possible?

Answer

Ajayi Oluwaseun Emmanuel picture Ajayi Oluwaseun Emmanuel · Dec 19, 2012
json_decode(json_encode((array) simplexml_load_string($obj)), 1);