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.
Did I help you?
[…] 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 just great and saved me soo much time. Thank you very much.
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!
This is awsome, thank you so much. As you have said in your article, it is amazing that PHP does not have array to XML and XML to array conversion functions. Your stuff does exactly what I need.
What license are you releasing this code under? Just wondering if it can be used in a commercial project.
Thanks!
Cheers,
Toby
Unfortunately, I don’t know. This isn’t my code. I got it from the code repository on the Zend site (link).
The author did not supply a license with the code, the submission form for code does not say that any license would be applied automatically to submitted code, and the site legal terms indicates that all user submitted content gives Zend royalty-free use of the submitted content. So, while all of that covers what Zend can do with the code, it does not give the community (us) an implicit or explicit license to use the code posted on the site unless the author supplies a license notice with their code (which some have).
As far as I can tell, this means that the author still holds all rights to the code. So, the author could press me to remove the code from my site if he chose to do so. I’ve tried finding and contacting the author to clarify the licensing terms but have not been able to get in contact with him.
The reason that I posted it here is because it is useful and code posted to such locations is done by authors so that others can use the code. So, while that doesn’t give me any legal right to redistribute the code, I don’t believe that the author has any intent to pursue damages against me.
All that said, I leave it up to the individual reader’s discretion how they decide to use the code.
Congratulations. Very thanks to your contribute.
[…] http://chrisjean.com/2009/01/12/convert-xml-to-associative-array-in-php/ […]
After messing with SimpleXML and other homerolled ugliness – I found this.
Just want I wanted.
Thank you, and thank you “nickc1998”.
Great help
Thanks for sharing with us
It saves my lot of time
Thanks ones again
Thank you. You are awesome!
Was having issues with XML to associative array in PHP
Fixed it 😀
Happy now.
Many thanks . This is what exectly i was looking for .
[…] http://chrisjean.com/2009/01/12/convert-xml-to-associative-array-in-php/ […]