PHP如何将XML转成数组
发布:smiling 来源: PHP粉丝网 添加日期:2019-10-10 18:36:23 浏览: 评论:0
如果你使用 curl 获取的 xml data
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 <?xml="" version="1.0" ?="">
- <document>
- <title>Forty What?</title>
- <from>Joe</from>
- <to>Jane</to>
- I know that's the answer -- but what's the question?
- </document>
- XML;
- $xml=simplexml_load_string($string);
- $data = json_decode(json_encode($xml),TRUE);
- var_dump( $xml );
- var_dump( $data );
- </xml>
- object(SimpleXMLElement)[1]
- public 'title' => string 'Forty What?' (length=11)
- public 'from' => string 'Joe' (length=3)
- public 'to' => string 'Jane' (length=4)
- public 'body' => string '
- I know that's the answer -- but what's the question?
- ' (length=57)
- array
- 'title' => string 'Forty What?' (length=11)
- 'from' => string 'Joe' (length=3)
- 'to' => string 'Jane' (length=4)
- 'body' => string '
- I know that's the answer -- but what's the question?
- ' (length=57)
以上就是本文的全部内容,希望对大家的学习有所帮助。
Tags: XML转成数组

推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)