PHP将XML数据转换为数组

<?php

$s=join(,file('httpapi.elong.comxmlv2.0hotelcn0132701501.xml'));
$result = xml_to_array($s);
print_r($result);

/*函数*/
function xml_to_array($xml)
{
    $array = (array)(simplexml_load_string($xml));
    foreach ($array as $key=$item){
        $array[$key]  =  struct_to_array((array)$item);
    }
    return $array;
}

function struct_to_array($item) {
    if(!is_string($item)) {
        $item = (array)$item;
        foreach ($item as $key=$val){
            $item[$key]  =  struct_to_array($val);
        }
    }
    return $item;
}

 

【2019年11月19日17:37:43】

$xml= "<xml><appid>123456</appid></xml>";//XML文件
$objectxml = simplexml_load_string($xml);//将文件转换成 对象
$xmljson= json_encode($objectxml );//将对象转换个JSON
$xmlarray=json_decode($xmljson,true);//将json转换成数组

 

posted @ 2016-10-28 18:09  码农骆驼  阅读(1667)  评论(0编辑  收藏  举报