How can I convert an array to a SimpleXML object in PHP?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- Illegal to have multiple roots (start tag in epilo
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
Another improvement:
Usage:
Here's a function that did the trick for me:
Just call it with something like
You could use the XMLParser that I have been working on.
Would result in:
IF the array is associative and keyed correctly, it would probably be easier to turn it into xml first. Something like:
The other route would be to create your basic xml first, like
and then for each part of your array, use something similar to my text creating loop and instead use the simplexml functions "addChild" for each node of the array.
I'll try that out later and update this post with both versions.
the following deals with namespaces. In this case, you construct the wrapper to include the namespace definitions, and pass it into the function. use a colon to identify the namespace.
Test Array
If you work in magento and you have this type of associative array
then this is best to convert associative array to xml format.Use this code in controller file.
I hope it helps to all.