PHP如何将XML转成数组
如果你使用curl获取的xmldata
xml=simplexmlloadstring(data);
data[′tk′]=jsondecode(jsonencode(xml),TRUE);
如果是直接获取URL数据的话
xml=simplexmlloadfile(data);
data[′tk′]=jsondecode(jsonencode(xml),TRUE);
先把simplexml对象转换成json,再将json转换成数组。
代码:
<?php $string=<<<XML <?xmlversion='1.0'?> <document> <title>FortyWhat?</title> <from>Joe</from> <to>Jane</to> <body> Iknowthat'stheanswer--butwhat'sthequestion? </body> </document> XML; $xml=simplexml_load_string($string); $data=json_decode(json_encode($xml),TRUE); var_dump($xml); var_dump($data);
object(SimpleXMLElement)[1] public'title'=>string'FortyWhat?'(length=11) public'from'=>string'Joe'(length=3) public'to'=>string'Jane'(length=4) public'body'=>string' Iknowthat'stheanswer--butwhat'sthequestion? '(length=57) array 'title'=>string'FortyWhat?'(length=11) 'from'=>string'Joe'(length=3) 'to'=>string'Jane'(length=4) 'body'=>string' Iknowthat'stheanswer--butwhat'sthequestion? '(length=57)
以上就是本文的全部内容,希望对大家的学习有所帮助。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短