1

How can I convert this PHP array into XML? I am using PHP codegniter. Is there any method or library for converting an array into XML? Here is my PHP array code. What am I doing wrong?

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
        )

)
Leo Izen
  • 4,165
  • 7
  • 37
  • 56
sourabh
  • 11
  • 4
  • 1
    post your code if you need help – Marco Salerno Mar 17 '17 at 11:01
  • Tip: Each `SimpleXMLElement` can be parsed as xml via `asXML();` also have a look here http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml#5965940 – JustOnUnderMillions Mar 17 '17 at 11:04
  • i am using this code for converting this array into xml public function array_to_xml( $data, &$xml_data ) { foreach( $data as $key => $value ) { if( is_numeric($key) ){ $key = 'item'.$key; //dealing with <0/>.. issues } if( is_array($value) ) { $subnode = $xml_data->addChild($key); array_to_xml($value, $subnode); } else { $xml_data->addChild("$key",htmlspecialchars("$value")); } } } – sourabh Mar 17 '17 at 11:10

1 Answers1

0

I found this by googling

How to convert array to SimpleXML

You have an array of simple SimpleXMLElement Objects

so

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

That should add <node0>, <node1>, <node2>

That is if $value behaves well. I did not test.