如何将这个 PHP 数组转换为 XML?我正在使用 PHP codegniter。是否有将数组转换为 XML 的方法或库?这是我的 PHP 数组代码。我做错了什么?
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
)
)
最佳答案
我通过谷歌搜索找到的
How to convert array to SimpleXML
您有一组简单的 SimpleXMLElement 对象
所以
$xml = new SimpleXMLElement('<root/>');
foreach ($my_array as $key => $value) {
$node = "node" . $key;
$xml->addChild($node, $value);
}
应该添加
如果 $value 表现良好。我没有测试。
关于php - 将php数组转换成xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855744/