Convert XML to Associative Array in PHP
I’m working on a project where I needed to convert an XML doc into an associative array in PHP easily. I hadn’t done any work with XML in PHP yet, so I started digging around the usual places.
Soon, it looked like my best option would be to create a completely-custom XML parsing engine out of PHP’s built-in XML Parser. I started working on a set of code based off of the External Entity Example since that is the only example that actually incorporates the ability to retrieve data and not just attributes. My results were slow and very error prone. Why is this so tough? XML isn’t exactly new, and it is very widespread. Similarly, PHP is an extremely popular language. So, why is it so hard to combine the two and work with them easily? I shouldn’t have to create a custom syntax parser just to be able to read a standard format document into a PHP data structure.
I started to look around again. That’s when I found PEAR’s XML_Parser. Great! This looks to be just what I want. It deals with all the junk, and tries its best to just let me traverse the document. I started using the examples and quickly realized something, XML_Parser may work great for a document definition that never changes, but I want something that isn’t rigidly tied down to a very legalistic expectation of data. Is it really so much to ask for just a function that can take the structure and throw it into an associative array so that I don’t have to recode the parser every time the data format changes?
So, my search began anew. This time, I found something that was perfect: two functions that could either take an associative array and dump out an XML string or take an XML string and dump out an associative array. I was amazed since it was exactly what I was looking for. My pain relief came from XML @=@ Associative Array.
Frankly, it looks like ignored code. It’s been around for nearly seven years on that site, yet it hasn’t even had 8000 views? What is wrong with people? IMHO, this type of functionality should be directly built into the core of PHP. Now I admit that this code isn’t everyone’s cup-o-tea, but it should work for about 95% of people that just want to use the data in an XML file quickly and easily into their code.
The code itself isn’t perfect, it doesn’t do enough checking, so it throws massive amounts of warnings. I also found the naming schemes a bit lacking and some of the formatting not quite to my liking. So, I cleaned it up and modified some of the naming conventions. While I was at it, I added the ability to load XML from a file or URL rather than just from a string. Apparently, this was intended based on the given examples but was never implemented. The code is a bit long to post here, so click the download link below to get the file.
Enjoy.
2009-02-11 Update
WordPress autosave fails me again. I had expanded this post by more than 100% with examples and details of the parameter options for each classes’ methods. Unfortunately, Flash crashed my browser. When I loaded back up, all my changes were nowhere to be found.
I guess that the autosave doesn’t work when you are updating a post. This really should be a feature where modifying a post that has already been published causes new revisions to be saved but not made active. This way I could recover data in the event of a browser crash.
Anyways. I updated the download above. The xmlLib.php file has been updated (check the file for update notes) and I’ve included a test file that will show you a quick example of how to use the two classes.
Tags: PHP, XMLShare This Post
Related Posts
Receive Updates
New posts on gaarai.com delivered directly to your email.






[...] here: Convert XML to Associative Array in PHP Related ArticlesBookmarksTags PHP PHP is a computer scripting language. Originally [...]
heroic, many thanks
This is *exactly* what I was looking for. Very much appreciated, thank you.
You’re welcome Spoom. I’m glad that it has helped.
Any examples to show on how it works etc?
You’re very right. I completely forgot to provide any examples. I’ll work on some and update the post.
Thanks for this class.
But I has a problem.
The class don’t list all my XML file but only the first item.
I would like to use a class for a menu build in XML and call an item by the name of the tag and it’s ID.
Could someone help me ?
P.S : Excuse my bad english
You need to tailor the code to the XML document. As you can see in my xmlLibTest.php file, I set “story” to “_array_”. That is because the story tag is what indicates the repeated entries.
In order to help you along, I’m going to send you an email. Please send me a reply with the XML document that you are working with, and I’ll see if I can produce some code that generates what you need.
thx
exactly what i am looking for
PHaCS
Glad to help PHaCS.
Great code, much appreciated. I have no problems converting XML -> Array and manipulating that, but I’m having some troubles with attributes and also converting the modified Array -> XML to save the file. If it’s not too much trouble, could you shoot me off an email so I can discuss in more depth?
Email sent.
This is great! I’m bit of a noob, but could you show me how to access/extract the variables from the results of the XMLToArray method?
Oops…nevermind, I figured it out. Thanks again for the code!