将php数组转换为xml文件

时间:2017-03-17 10:59:31

标签: php arrays xml xml-parsing

如何将此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
        )

)

1 个答案:

答案 0 :(得分:0)

我通过谷歌搜索找到了这个

How to convert array to SimpleXML

你有一组简单的SimpleXMLElement对象

所以

$xml = new SimpleXMLElement('<root/>');
foreach ($my_array as $key => $value) {
    $node = "node" . $key;
    $xml->addChild($node, $value);
}

那应该添加&lt; node0&gt;,&lt; node1&gt;,&lt; node2&gt;

即如果$ value表现良好。我没有测试。