我的基本思路是遍历数组 判断当前元素有没有名为"childs"的子数组 如果有的话 递归执行本函数
为什么生成的xml不对? items跑到了外面 如图
$config = array(
'root' => array(
'childs' => array(
'body' => array(
'childs' => array(
'scroller' => array(
'childs' => array(
'header' => array(
'childs' => array(
'simpleHeader' => array(
'childs' => array(
'a' => array()
)
)
)
),
'items' => array(
'childs' => array(
'fuck' => array(
'childs' => array(
'b' => array()
)
)
)
)
)
)
)
)
)
)
);
header("Content-type:text/xml;Charset=UTF-8");
$xml = new XMLWriter();
$xml->openUri('php://output');
$xml->startDocument('1.0', 'UTF-8');
$xml->setIndent(true);
function writeXml($array){
global $xml;
foreach($array as $key => $value){
$xml->startElement($key);
if( isset($value['childs']) && is_array($value['childs']) && count($value['childs']) > 0 ){
writeXml($value['childs']);
}
$xml->endElement();
}
$xml->endDocument();
$xml->flush();
}
writeXml($config);
为什么生成的xml不对? items跑到了外面 如图
![](http://img.bbs.csdn.net/upload/201404/16/1397617569_179066.jpg)