Continue yesterday's PHP multidimensional array conversion to XML data format related issues

Source: Internet
Author: User
Tags addchild php template
Continuing the problem of converting PHP multidimensional arrays into XML data formats yesterday
First of all, thanks to yesterday's two friends: jordan102 and Xuzuning
Posts Link:

Their two-bit method of testing is indeed feasible, indeed.

But in the course of the development of the project, it was found that the project was so perverted that there was such a situation

PHP Array:

"Auth" = = Array (
"User" = "user001",
"Password" = "pass001",
"Context" =
"Task" = = Array (
"Code" = "0101",
"Item" =>array (
"title" = "abc",
"Web" =>array (
"Name" = "",
"IP" = "",
"Web" =>array (
"Name" = "",
"IP" = "",
"Confirm Order" = "1",
"Reply_to" = "",

Note that underneath the item node, there are two child nodes of the web, in fact there may be several web sub-nodes, typically around 2-4

Now the converter code is as follows:
$xml = simplexml_load_string ("
Create ($ar, $xml);
$xmlData = $xml->savexml ();
Echo $xmlData;

function Create ($ar, $xml) {
foreach ($ar as $k = = $v) {
if (Is_array ($v)) {
$x = $xml->addchild ($k);
Create ($v, $x);
}else $xml->addchild ($k, $v);

This code is relatively efficient, but in the encounter of the same name node, the filter is only the last node, so the result of the final output becomes, notice that the Web name value of is missing.

The output turns into:





The conversion of the function, I changed and changed, or have been unable to output the full data, headache ...

What is the best way for you to ask your predecessors?

Share to:

Your array does not exist, and it is not possible to have two identical key names in the same dimension array.
The question is what is the entry data? Text? Json? It's definitely not the array you showed.

Ready-made Array2xml, Json2xml Class A lot of AH
Your problem now is not the problem with the array transformation XML, but the problem when the array is generated.
Or that an array in PHP cannot represent your most primitive data (at least not in the way you're currently using it)
So please post the most original data, change the way it is expressed, to solve the problem.

Not the other person's entrance, is your entrance, how did your array come from? Can't have the same key
You're not as complicated as XML, it doesn't involve the escaping of Chinese and special characters.
If you are unfamiliar with XML, it might be easier to generate XML by looping through string concatenation.
Quote: Reference:

Not the other person's entrance, is your entrance, how did your array come from? Can't have the same key

Well, after the advice of your predecessors, it is clear that I have already defined the same key-value name when creating the PHP array and assigning values, causing problems with the array.

I now find a more stupid solution, is to define a text variable $xmltext, the other request to send the text of the XML data copied, and then use the replacement of several variables in the text, replaced, directly post the past, do not need to define the array, and then converted to XML.

This is not stupid, in fact, is the PHP template approach, do not forget the output header->text/xml
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.