PHP array to XML and XML transform array instance _php tutorial

Source: Internet
Author: User
Tags addchild php xml to array
This article to the students to introduce two simple examples, PHP array to XML and XML conversion array, I hope this article will help you friends.

PHP Array to XML

The code is as follows Copy Code

function Array2xml ($array, $xml = False) {
if ($xml = = = False) {
$xml = new SimpleXMLElement (" );
}
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 array

The code is as follows Copy Code

$s = <<


Basic
1
4
3
1
2


EOS;

$a = Json_decode (Json_encode ((array) simplexml_load_string ($s)), 1);
Print_r ($a);

http://www.bkjia.com/PHPjc/632670.html www.bkjia.com true http://www.bkjia.com/PHPjc/632670.html techarticle This article to the students to introduce two simple examples, PHP array to XML and XML conversion array, I hope this article will help you friends. PHP array to XML code to copy code as follows ...

  • 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.