Continue to convert yesterday's PHP multi-dimensional array into XML data format

Source: Internet
Author: User
To continue the PHP multi-dimensional array conversion to XML data format yesterday, first of all, thanks to the two friends yesterday: jordan102 and xuzuning post links: http://bbs.csdn.net/topics/390609227their two-digit ratio test is feasible. However, during the development of the project, it was found that the project was too abnormal, and the problem of converting the PHP multi-dimensional array to XML data format yesterday was still solved.
First of all, I would like to thank two friends yesterday: jordan102 and xuzuning.
Link: http://bbs.csdn.net/topics/390609227

The methods provided by the two of them are indeed feasible and practical.

However, during the development of the project, we found that the project was too abnormal.

PHP array:
$ar=array( 
"auth"=> array(
"user"=>  "user001",
"password"=> "pass001",
"context"=>  "1" 
),
"task"=> array(
"code"=>"0101",
"item"=>array(
"title"=>"abc",
"web"=>array(
"name"=>"baidu.com",
"ip"=>"1.2.3.4",
),
"web"=>array(
"name"=>"sina.com",
"ip"=>"1.1.1.1",
),
"confirm order"=>"1",
),
"reply_to"=>"info@xxx.com",
),
);


Note that there are two web subnodes under the item node. In fact, there may be several web subnodes, usually around 2-4.

The current conversion program 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 more efficient, but when you encounter nodes with the same name, it filters out and retains only the last node, so that the final output result is changed, if you set the name of the web to baidu.com, this will be omitted.

The output result is:
 


user001
pass001
1


0101

abc

sina.com
1.1.1.1

1

info@xxx.com



I have changed and changed the conversion function, but I still cannot output the complete data, causing a headache ......

What are the best methods for your predecessors?
Share:
------ Solution --------------------
Your array does not exist. two identical key names cannot appear in the same dimension array.
------ Solution --------------------
The question is, what is the portal data? Text? Json? Definitely not the array you display

There are many ready-made array2xml and json2xml classes.
------ Solution --------------------
Your current problem is not the problem of converting arrays into xml, but the problem has already occurred when the array is generated.
Or the array in php cannot represent your original data (at least this method you currently use cannot represent)
Therefore, paste the original data and change its representation to solve the problem.

------ Solution --------------------
It's not the opposite's portal, it's your Portal. How did you get your array? It is impossible to have the same key
------ Solution --------------------
Your xml does not seem to be so complicated. it does not involve escape of Chinese characters or special characters.
If you are not familiar with xml, it is easier to generate xml by concatenating strings cyclically.
------ Solution --------------------
Reference:
Quote: reference:

It's not the opposite's portal, it's your Portal. How did you get your array? It is impossible to have the same key


Well, after some previous instructions, I understand that the same key value name has been defined when I created a php array and assigned a value, which leads to an array problem.

Now I have found a stupid solution: first define a text variable $ xmltext, copy the xml data text requested by the other party, and then use several variables in the replaced text, after replacement, post the file directly. you do not need to define the array and convert it to xml.

This is not stupid. it is actually the php template practice. Don't forget to output header-> text/xml

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: info-contact@alibabacloud.com 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.