将php数组转换为xml文件
如何将此PHP数组转换为XML?我使用的是PHP代码。是否有将数组转换为XML的方法或库?这是我的PHP数组代码。我做错了什么将php数组转换为xml文件,php,arrays,xml,xml-parsing,Php,Arrays,Xml,Xml Parsing,如何将此PHP数组转换为XML?我使用的是PHP代码。是否有将数组转换为XML的方法或库?这是我的PHP数组代码。我做错了什么 Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1 ) [timecode]
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[timecode] => 12:23:55:142
[datetimecode] => 11:30:01:06 2016/10/14
[color] => Green
[user] => logger 1
[comment] => Jdjd
[framecode] => 1115899
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 2
)
[timecode] => 06:12:04:02
[datetimecode] => 11:30:05:15 2016/10/14
[color] => Magenta
[user] => logger 1
[comment] => Ndnnd
Ndnnd
[framecode] => 558109
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 3
)
[timecode] => 06:12:13:17
[datetimecode] => 12:32:34:07 2016/10/14
[color] => White
[user] => logger 1
[comment] => Dd
[framecode] => 558349
)
)
我通过谷歌搜索找到了这个 您有一个简单SimpleXMLElement对象数组 所以
$xml=新的SimpleXMLElement(“”);
foreach($key=>$value的my_数组){
$node=“node”。$key;
$xml->addChild($node,$value);
}
这应该补充一点
也就是说,如果$value表现良好。我没有测试。如果需要帮助,请发布代码提示:每个
simplexmlement
都可以通过asXML()解析为xml代码>也看看这里,我正在使用此代码将此数组转换为xml公共函数数组。\u to_xml($data,&$xml_data){foreach($key=>data$value){if(is_numeric($key)){$key='item'.$key;//处理..问题}if(is_array($value)){$subnode=$xml\u data->addChild($key);array\u to_xml($value,$subnode);}else{$xml\u data->addChild($key),htmlspecialchars($value”);}
$xml = new SimpleXMLElement('<root/>');
foreach ($my_array as $key => $value) {
$node = "node" . $key;
$xml->addChild($node, $value);
}