Heim  >  Artikel  >  php教程  >  php将xml数据转化为数组(array)

php将xml数据转化为数组(array)

WBOY
WBOYOriginal
2016-06-06 19:55:272151Durchsuche

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 用php实现将xml数据转化为数组(array): 将xml转化为数组,使用正则表达式对xml数据进行匹配.代码如下: function xml_to_array( $xml ){ $reg = “/(\w+)[^]*([\x00-\xFF]*)\/\1/”; if(preg_match_all

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

    用php实现将xml数据转化为数组(array):

    将xml转化为数组,使用正则表达式对xml数据进行匹配.代码如下:

    function xml_to_array( $xml ){

    $reg = “/]*>([\x00-\xFF]*)/”;

    if(preg_match_all($reg, $xml, $matches))

    {

    $count = count($matches[0]);

    for($i = 0; $i

    {

    $subxml= $matches[2][$i];

    $key = $matches[1][$i];

    if(preg_match( $reg, $subxml ))

    {

    $arr[$key] = xml_to_array( $subxml );

    }else{

    $arr[$key] = $subxml;

    }

    }

    }

    return $arr;

    }

    function xmltoarray( $xml ){

    $arr = xml_to_array($xml);

    $key = array_keys($arr);

    return $arr[$key[0]];

    }

php将xml数据转化为数组(array)

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn