簡體   English   中英

將php數組轉換為xml文件

[英]Convert php array into xml file

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

)

我通過谷歌搜索找到了這個

如何將數組轉換為SimpleXML

你有一個簡單的SimpleXMLElement對象數組

所以

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

那應該添加<node0>,<node1>,<node2>

那就是$ value表現良好。 我沒有測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM