php - 将php数组转换成xml文件

标签 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
        )

)

最佳答案

我通过谷歌搜索找到的

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/

相关文章:

python - 用零填充 numpy 数组,并使用另一个数组作为 1 的索引

php - Laravel 更新要求在保存到数据库之前填写所有输入

c++ - 从字符串数组中获取字符值

java - 在 Tomcat7 服务器上用 Java 创建新实例时出现 XmlPullParser 异常

html - 没有特定类后代的所有元素的 XPath?

PHP shell_exec 使用 ssh 运行 shell 脚本

php - 使用 PHP 动态创建数组

android - 为什么android无法解析符号但它仍然有效?

javascript - 循环隐藏和显示表格行

php - 如何在 php 中嵌入 python 脚本?