php将xml转换为数组

发布时间:2020-03-28 06:32:31 作者:Lee_吉
来源:网络 阅读:1288
  1. 函数:
    /*
    @desc:xml转数组
    @param data xml字符串
    @return arr 解析出的数组
    */
    function xmltoarray($data){
    $obj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
    $json = json_encode($obj);
    $arr = json_decode($json, true);      
    return $arr;
    }
  2. 测试:
    a. 代码:
    <?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;
    $arr = xmltoarray($string);
    var_dump($arr);

    b. 输出:

    array(4) {
    ["title"]=>
    string(11) "Forty What?"
    ["from"]=>
    string(3) "Joe"
    ["to"]=>
    string(4) "Jane"
    ["body"]=>
    string(57) "
    I know that's the answer -- but what's the question?
    "
    }
推荐阅读:
  1. php数组转xml
  2. 如何将xml和jsonp转换为PHP数组

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php xml 数组

上一篇:web上种图片应用的优缺点

下一篇:NetApp CIFS文件共享创建

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》