Home  >  Article  >  php教程  >  php将xml数据转化为数组(array)

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

WBOY
WBOYOriginal
2016-06-06 19:55:272212browse

欢迎进入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)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn