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 ');p rint_r ($SX);p Rint_r (ToArray ($SX));//Look at the source code can see that the return of the page is Error $arr=array (' A ' = ' 111 ', ' b ' = ' 2222 ', ' C ' =>array (' d ' = ' = ' hahaha ', ' e ' =>array (' what ' = ' the ' x ')));//array to XML, new array added to new XML, Arr to Xmlfunction ToXml ($arr, $node =null) {//Create 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, build nodes to put things in this array-"V is the number group,//large sections name is K, to the next back , put things into this sim-"AddChild toxml ($v, $simxml->addchild ($k));//If the array key is a number, you should give him a label name, because the direct use of numbers will be problematic}else if (Is_numeric ($ k) {$simxml->addchild (' item ', $v);} else{//is not an array, just add the child element key name-key value $simxml->addchild ($k, $v);}} return $simxml->savexml ();} Header (' Content-type:text/xml '); Echo ToXml ($arr);? >
The above describes the transformation of XML and arrays, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.