//XML转数组,第一种 $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); //XML转数组,第二种 $arr = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); //数组转XML $xml = simplexml_load_string('<root />'); create($arr, $xml); $xml = $xml->saveXML(); function create($arr, $xml) { foreach($arr as $k=>$v) { if(is_array($v)) { if(array_keys($v) !== range(0, count($v)-1)) { //关联数组 $x = $xml->addChild($k); create($v, $x); } else { //索引数组 $x = $xml->addChild($k); foreach ($v as $vv) { $y = $x->addChild($vv['nodename']); //要有节点名称 unset($vv['nodename']); create($vv, $y); } } } else { $xml->addChild($k, $v); } } }