Categories
PHP Programming

Convert XML to array and vice versa in PHP

As a developer, it is common to search the web for an existing (open source) code snippet to re-use for an application, but it is rare for the developer to just copy code online and use it straight in their application without modifying it first.

I am one of many developers who like to save time if possible by using open source code if coding from scratch takes longer than a couple of minutes, but there are times when one cannot be found. I ran into such situation when I wanted a function that converts an XML string into an array. Unfortunately, most are overly complicated for what I wanted, so I decided to create my own and share with the world. The following function converts XML strings into a PHP array while retaining the level depths of the XML but all attributes are ignored.

Advertisement