如果你使用 curl 获取的 xml data
$xml = simplexml_load_string($data);
$data['tk'] = json_decode(json_encode($xml),TRUE);
如果是直接获取 URL 数据的话
$xml = simplexml_load_file($data);
$data['tk'] = json_decode(json_encode($xml),TRUE);
先把 simplexml 对象转换成 json,再将 json 转换成数组。

<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </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 '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)

PHP将XML转成数组的更多相关文章

  1. XML转换成数组方法

    <?php function xmlToArray2($xml) { // 将XML转为array $array_data = json_decode(json_encode(simplexml ...

  2. PHP如何将XML转成数组

    如果你使用 curl 获取的 xml data $xml = simplexml_load_string($data); $data['tk'] = json_decode(json_encode($ ...

  3. xml转换成数组array

    直接上代码,成功转换 if($data){ //返回来的是xml格式需要转换成数组再提取值,用来做更新 $startnum = strpos($data,"<xml>" ...

  4. php十行代码将xml转成数组

    <?php header("Content-Type:text/html;charset=utf-8"); function xml2array($filename){ $x ...

  5. xml转成数组,原来这么简单!

    function xml2arr($xml){ $obj  = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $js ...

  6. 非常简单的 xml转成数组的方法

    function xml2arr($xml){ $obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $jso ...

  7. 单行代码实现xml转换成数组

    $string = '<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_m ...

  8. xml格式的数据转化成数组

    将得到的xml格式的数据转化成数组 <?php //构造xml $url = "http://api.map.baidu.com/telematics/v3/weather?locat ...

  9. 将xml转换成Json,数组,对象格式转换方法

    xml字符串:$simplexml 转换成Json格式:json_encode($simplexml) 转换成数组格式:json_decode(json_encode($simplexml),TRUE ...

随机推荐

  1. rotate the clock

    A program test: You are given N round clocks. Every clock has M hands, and these hands can point to ...

  2. [转载] 1. JebAPI 之 jeb.api

    本文转载自: https://www.zybuluo.com/oro-oro/note/142707 JEB API 官方地址:https://www.pnfsoftware.com/apidoc/  ...

  3. python 文件拷贝

    用python实现了一个小型的自动发版本的工具.这个“自动发版本”有点虚, 只是简单地把debug 目录下的配置文件复制到指定目录,把Release下的生成文件复制到同一指定,过滤掉不需要的文件夹(. ...

  4. echo颜色显示

    echo -e "\033[30m 黑色字 \033[0m" echo -e "\033[31m 红色字 \033[0m" echo -e "\033 ...

  5. android开发环境搭建(64位)

    一.相关下载 1.下载JDK.  网址:http://java.sun.com/javase/downloads/index.jsp,选择jdk-7u21-windows-x64.exe. 2.下载E ...

  6. Z Fighting Problem

    Here is a video about unity depth shader workarounds: http://www.burgzergarcade.com/tutorials/game-e ...

  7. SQL Server 判定时间是否在某个时间区间内

    * FROM sys.objects WHERE name=N'uF_IsRange_Date' AND [type]='FN') DROP FUNCTION uF_IsRange_Date GO S ...

  8. vim 分屏

    分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 file2 ... 注释: n是数字,表示分成几 ...

  9. [转]Sql server2005中如何格式化时间日期

    ) -- mon dd yyyy hh:mmAM (or PM) ) -- mm/dd/yyyy - 10/02/2008 ) -- yyyy.mm.dd -- 2008.10.02 ) -- dd/ ...

  10. 自动化运维工具之ansible(转)

    原文链接:http://os.51cto.com/art/201409/451927_all.htm