首页 > 代码库 > PHP将XML数据转换为数组

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;
}

 

PHP将XML数据转换为数组