-
MYYA
没有现成函数,只能自己写;我有一个别人写的函数:<?phpclass A2Xml {private $version = '1.0';private $encoding = 'UTF-8';private $root = 'root';private $xml = null;function __construct() {$this->xml = new XmlWriter();}function toXml($data, $eIsArray=FALSE) {if(!$eIsArray) {$this->xml->openMemory();$this->xml->startDocument($this->version, $this->encoding);$this->xml->startElement($this->root);}foreach($data as $key => $value){if(is_array($value)){$this->xml->startElement($key);$this->toXml($value, TRUE);$this->xml->endElement();continue;}$this->xml->writeElement($key, $value);}if(!$eIsArray) {$this->xml->endElement();return $this->xml->outputMemory(true);}}}$res = array('hello' => '11212','world' => '232323','array' => array('test' => 'test','b' => array('c'=>'c', 'd'=>'d')),'a' => 'haha');$xml = new A2Xml();echo $xml->toXml($res);
-
弑天下
123456789101112131415161718192021222324252627282930313233343536373839404142<?phpclass A2Xml { private $version = '1.0'; private $encoding = 'UTF-8'; private $root = 'root'; private $xml = null; function __construct() { $this->xml = new XmlWriter(); } function toXml($data, $eIsArray=FALSE) { if(!$eIsArray) { $this->xml->openMemory(); $this->xml->startDocument($this->version, $this->encoding); $this->xml->startElement($this->root); } foreach($data as $key => $value){ if(is_array($value)){ $this->xml->startElement($key); $this->toXml($value, TRUE); $this->xml->endElement(); continue; } $this->xml->writeElement($key, $value); } if(!$eIsArray) { $this->xml->endElement(); return $this->xml->outputMemory(true); } }}$res = array( 'hello' => '11212', 'world' => '232323', 'array' => array( 'test' => 'test', 'b' => array('c'=>'c', 'd'=>'d') ), 'a' => 'haha');$xml = new A2Xml();echo $xml->toXml($res);
-
白衣染霜花
xml转array方法没错,只是xml中有三个<list>,而数组中却不能出现三个$arr['list'],所以这个方法自动把三个<list>中的内容放进了一个二维数组中。可以尝试直接取$arr['list'],取出结果应该就是 Array ( [0] => 1 [1] => 2 [2] => 3 ) 了。
-
Helenr
public function arrayToXml($arr){$xml = "<xml>";foreach ($arr as $key=>$val){if(is_array($val)){$xml.="<".$key.">".arrayToXml($val)."</".$key.">";}else{if (is_numeric($val)){$xml.="<".$key.">".$val."</".$key.">";}else{$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";}}}$xml.="</xml>";return $xml;}