I know I'm 1 billion years late, but I had the same problem as you and needed a more complex solution, so here is a function [xml_decode[]
] I made to convert SimpleXMLElements
into PHP arrays without losing attributes and with arguments for a more customizable use.
The val[]
function is meant for you to customize how element values shall be treated - in case you want to transform true
into true
instead of "true"
for example.
Disclaimer: I know it is easier to just use the PHP SimpleXML extension, but I needed to transform lots of XML files into JSON files for a big change in a project of mine. Also, the question is about how to transform XML to PHP arrays, not how to use XML in PHP.