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)
以上就是本文的全部内容,希望对大家的学习有所帮助。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下