这是我用于从多维数组创建 XML 的解决方案。
<?php
$xmlDAta = array(
array(
"name" => "nameVal",
"value" => "valVal",
"css" => "cssVal"
),
array(
"name" => "name1Val",
"value" => "val1Val",
"css" => "css1Val"
),
"tname" => array(
array(
"iTname" => "iTname",
"iTname2" => "iTname1",
"iTname2" => "iTname2",
"iTbname3" => array(
"iiTbname" => "tbName",
"iiTbname1" => "tbName1",
),
),
),
"tdata" => "otheerDAta"
);
function data2XML(array $data, SimpleXMLElement $xml, $child = "items")
{
foreach($data as $key => $val) {
if(is_array($val)) {
if(is_numeric($key)) {
$node = $xml->addChild($child);
$nodes = $node->getName($child);
} else {
$node = $xml->addChild($key);
$nodes = $node->getName($key);
}
$node->addChild($nodes, self::data2Xml($val, $node));
} else {
$xml->addChild($key, $val);
}
}
}
$xml = new SimpleXMLElement("<root/>");
Util::data2XML($xmlDAta, $xml, "Items");
?>