Convert XML and array

Source: Internet
Author: User
: This article mainly describes how to convert XML and arrays. For more information about PHP tutorials, see. Two methods: Recursive conversion of arrays and xml

 $ V) {if ($ v instanceof simplexmlelement | is_array ($ v) {$ sim [$ k] = toarray ($ v) ;}} return $ sim ;} $ sx = simplexml_load_file ('. /try. XML'); print_r ($ sx); print_r (toarray ($ sx); // you can see the source code, error $ arr = array ('a' => '000000', 'B' => '000000 ', 'C' => array ('D' => 'hahahaha', 'E' => array ('who' => 'The X '))); // Convert the array to xml, and add the new array to the new xml. arr to xmlfunction toxml ($ arr, $ node = null) {// create a new xml file if ($ node = null) {$ simxml = new simpleXMLElement ('
 
 ');} Else {$ simxml = $ node;} // foreach ($ arr as $ k => $ v) {if (is_array ($ v )) {// addChild (node name, content) // if it is an array, you need to create a node and put the content in this array -- "v is an array, // The main node name is k. next time, put the things into this sim-> addchild toxml ($ v, $ simxml-> addChild ($ k )); // if the array key is a number, you need to give it a tag name, because using a number directly causes problems} else if (is_numeric ($ k )) {$ simxml-> addChild ('item', $ v );} else {// add the child element key name-key value $ simxml-> addChild ($ k, $ v) directly if it is not an array );}} return $ simxml-> saveXML ();} header ('content-type: text/XML'); e Cho toxml ($ arr);?>

The above section describes how to convert XML and arrays, including some content. I hope my friends who are interested in PHP tutorials can help me.

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.