美文网首页
php把xml数据转化为数组

php把xml数据转化为数组

作者: 风度翩翩的程序猿 | 来源:发表于2021-04-15 13:54 被阅读0次
//将 xml数据转换为数组格式。
    private function xml_to_array($xml)
    {
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if (preg_match_all($reg, $xml, $matches)) {
            $count = count($matches[0]);
            for ($i = 0; $i < $count; $i++) {
                $subxml = $matches[2][$i];
                $key = $matches[1][$i];
                if (preg_match($reg, $subxml)) {
                    $arr[$key] = $this->xml_to_array($subxml);
                } else {
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }

相关文章

网友评论

      本文标题:php把xml数据转化为数组

      本文链接:https://www.haomeiwen.com/subject/jsmdlltx.html