An example of an array of PHP to convert XML to XML

Source: Internet
Author: User
Tags addchild

PHP Array to XML

The code is as follows Copy Code

function Array2xml ($array, $xml = False) {
if ($xml = = False) {
$xml = new SimpleXMLElement (' <root/> ');
}
foreach ($array as $key => $value) {
if (Is_array ($value)) {
Array2xml ($value, $xml->addchild ($key));
}else{
$xml->addchild ($key, $value);
}
}
return $xml->asxml ();
}

Header (' Content-type:text/xml ');
Print Array2xml ($array);

PHP XML to an array

  code is as follows copy code

$s = < <<eos
<root>
<Formula>
<formulaname>Basic</formulaname>
< Movespeed>1</movespeed>
<box>4</box>
<chicken>3</chicken>
<ducks >1</ducks>
<cereal>2</cereal>
</Formula>
</root>
EOS;
 
$a = Json_decode (Json_encode (array) simplexml_load_string ($s)), 1;
Print_r ($a);

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.