<?php function arrtoxml($arr, $node = 'root') { if ($node == 'root') { $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><root></root>'); } else { $xml = $node; } foreach ($arr as $key => $value) { if (!is_array($value)) { $xml->addChild(is_string($key) ? $key : 'item', $value); } else { arrtoxml($value, $xml->addChild($key)); //创建节点 并加在他的后面 } } $xml->asXML('arrtoxml.xml'); return $xml->asXML(); } $res = array( 'hello' => '11212', 'world' => '曹姐', 'array' => array( 'test' => 'test', 'b' => array('c' => 'c', 'd' => 'd') ), 'a' => 'haha', 'jie', 'yamin' ); echo arrtoxml($res); echo json_encode(simplexml_load_string(arrtoxml($res)), JSON_UNESCAPED_UNICODE); $xml = simplexml_load_file('arrtoxml.xml'); var_dump(json_decode(json_encode($xml, JSON_UNESCAPED_UNICODE), true));
07-26
07-26