Don’t want to read everything? Jump to the code or leave a comment
9 months ago I wrote a few lines PHP-code to get XML data into an array. The reason why I wrote this is hard to recall but in the process I had to solve a problem: how do I get a serie of values from an XML file into a PHP array?
Lots of solutions exist like the package PEAR::XML_Serializer but they were all too advanced for what I needed.
All I needed was a simple function or class that would do just that: convert xml file with values to php array. And I found a simple trick using nothing more than XSLT.
If you look at an array in PHP, the data inside is a storable presentation of a value
. To get the value back into a string you have to call the serialize() function. Getting the array back from the string, you guessed it, call the unserialize() function. I’m not getting any deeper into this, the manual entries are very clear and the array section of the PHP manual is very good.
Once you know how a serialized array looks like, you can generate it with whatever you have. You can generate it through an XSLT transformation, like I did.
(more…)