3
如何將此PHP數組轉換爲XML?我正在使用PHP codegniter。有沒有方法或庫將數組轉換爲XML?這是我的PHP數組代碼。我究竟做錯了什麼?將php數組轉換爲xml文件
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
)
)
如果您需要幫助,請發佈您的代碼 –
提示:每個'SimpleXMLElement'都可以通過asXML()解析爲xml;'也可以看看這裏http://stackoverflow.com/questions/1397036/how-to-轉換-陣列到simplexml的#5965940 – JustOnUnderMillions
我使用此代碼爲這個陣列轉換成XML 公共函數array_to_xml($數據,&$ xml_data){ 的foreach($數據作爲$密鑰=> $值){ if(is_numeric($ key)){$ key ='item'。$ key; //處理<0/> .. 問題 } if(is_array($ value)){ $ subnode = $ xml_data-> addChild($ key); array_to_xml($ value,$ subnode); } else { $ xml_data-> addChild(「$ key」,htmlspecialchars(「$ value」)); } } } –
sourabh