検索
ホームページphp教程php手册ThinkPHP XML格式转换成数组

XML格式转换成数组 用递归操作很简单,大家可以下载附件测试
// Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误<br> public function xml_to_array( $xml )<br>     {<br>         $reg = "/]*?>([\\x00-\\xFF]*?)/";<br>         if(preg_match_all($reg, $xml, $matches))<br>         {<br>             $count = count($matches[0]);<br>             $arr = array();<br>             for($i = 0; $i              {<br>                 $key = $matches[1][$i];<br>                 $val = $this->xml_to_array( $matches[2][$i] );  // 递归<br>                 if(array_key_exists($key, $arr))<br>                 {<br>                     if(is_array($arr[$key]))<br>                     {<br>                         if(!array_key_exists(0,$arr[$key])) <br>                         {<br>                             $arr[$key] = array($arr[$key]);<br>                         }<br>                     }else{<br>                         $arr[$key] = array($arr[$key]);<br>                     }<br>                     $arr[$key][] = $val;<br>                 }else{<br>                     $arr[$key] = $val;<br>                 }<br>             }<br>             return $arr;<br>         }else{<br>             return $xml;<br>         }<br>     }<br> <br> <br> // Xml 转 数组, 不包括根键<br> public function xmltoarray( $xml )<br>     {<br> <br>         $arr = $this->xml_to_array($xml);<br>         $key = array_keys($arr);<br>         return $arr[$key[0]];<br>     }第二种方法,预防递归溢出class XML<br> {<br> <br> public $parser = NULL;<br> public $document = NULL;<br> public $parent = NULL;<br> public $stack = NULL;<br> public $last_opened_tag = NULL;<br> <br> public function XML( )<br>     {<br>         $this->parser = xml_parser_create( );<br>         xml_parser_set_option( $this->parser, XML_OPTION_CASE_FOLDING, false );<br>         xml_set_object( $this->parser, $this );<br>         xml_set_element_handler( $this->parser, "open", "close" );<br>         xml_set_character_data_handler( $this->parser, "data" );<br>     }<br> <br> public function destruct( )<br>     {<br>         xml_parser_free( $this->parser );<br>     }<br> <br> public function &parse( &$data )<br>     {<br>         $this->document = array( );<br>         $this->stack = array( );<br>         $this->parent =& $this->document;<br>         $parsedata = xml_parse( $this->parser, $data, true ) ? $this->document : NULL;<br>         return $parsedata;<br>     }<br> <br> public function open( &$parser, $tag, $attributes )<br>     {<br>         $this->data = "";<br>         $this->last_opened_tag = $tag;<br>         if ( is_array( $this->parent ) && array_key_exists( $tag, $this->parent ) )<br>         {<br>             if ( is_array( $this->parent[$tag] ) && array_key_exists( 0, $this->parent[$tag] ) )<br>             {<br>                 $key = count_numeric_items( $this->parent[$tag] );<br>             }<br>             else<br>             {<br>                 if ( array_key_exists( "{$tag} attr", $this->parent ) )<br>                 {<br>                     $arr = array(<br>                         "0 attr" => $this->parent["{$tag} attr"],<br>                         $this->parent[$tag]<br>                     );<br>                     unset( $this->parent["{$tag} attr"] );<br>                 }<br>                 else<br>                 {<br>                     $arr = array(<br>                         $this->parent[$tag]<br>                     );<br>                 }<br>                 $this->parent[$tag] =& $arr;<br>                 $key = 1;<br>             }<br>             $this->parent =& $this->parent[$tag];<br>         }<br>         else<br>         {<br>             $key = $tag;<br>         }<br>         if ( $attributes )<br>         {<br>             $this->parent["{$key} attr"] = $attributes;<br>         }<br>         $this->parent =& $this->parent[$key];<br>         $this->stack[] =& $this->parent;<br>     }<br> <br> public function data( &$parser, $data )<br>     {<br>         if ( $this->last_opened_tag != NULL )<br>         {<br>             $this->data .= $data;<br>         }<br>     }<br> <br> public function close( &$parser, $tag )<br>     {<br>         if ( $this->last_opened_tag == $tag )<br>         {<br>             $this->parent = $this->data;<br>             $this->last_opened_tag = NULL;<br>         }<br>         array_pop( $this->stack );<br>         if ( $this->stack )<br>         {<br>             $this->parent =& $this->stack[count( $this->stack ) - 1];<br>         }<br>     }<br> <br> }<br> <br> function &XML_unserialize( &$xml )<br> {<br>     $xml_parser = new XML( );<br>     $data =& $xml_parser->parse( $xml );<br>     $xml_parser->destruct( );<br>     return $data;<br> }<br> <br> function &XML_serialize( &$data, $level = 0, $prior_key = NULL )<br> {<br>     if ( $level == 0 )<br>     {<br>         ob_start( );<br>         echo "<?xml version=\"1.0\" ?>";<br>         echo "\n";<br>     }<br>     while ( list( $key, $value ) = each( $data ) )<br>     {<br>         if ( !strpos( $key, " attr" ) )<br>         {<br>             if ( is_array( $value ) && array_key_exists( 0, $value ) )<br>             {<br>                 xml_serialize( $value, $level, $key );<br>             }<br>             else<br>             {<br>                 $tag = $prior_key ? $prior_key : $key;<br>                 echo str_repeat( "\t", $level );<br>                 echo "                 echo $tag;<br>                 if ( array_key_exists( "{$key} attr", $data ) )<br>                 {<br>                     while ( list( $attr_name, $attr_value ) = each( $data["{$key} attr"] ) )<br>                     {<br>                         echo " ";<br>                         echo $attr_name;<br>                         echo "=\"";<br>                         echo htmlspecialchars( $attr_value );<br>                         echo "\"";<br>                     }<br>                     reset( $data["{$key} attr"] );<br>                 }<br>                 if ( is_null( $value ) )<br>                 {<br>                     echo " />\n";<br>                 }<br>                 else if ( !is_array( $value ) )<br>                 {<br>                     echo ">";<br>                     echo htmlspecialchars( $value );<br>                     echo "{$tag}>\n";<br>                 }<br>                 else<br>                 {<br>                     echo ">\n";<br>                     echo xml_serialize( $value, $level + 1 );<br>                     echo str_repeat( "\t", $level );<br>                     echo "{$tag}>\n";<br>                 }<br>             }<br>         }<br>     }<br>     reset( $data );<br>     if ( $level == 0 )<br>     {<br>         $str =& ob_get_contents( );<br>         ob_end_clean( );<br>         return $str;<br>     }<br> }<br> <br> function count_numeric_items( &$array )<br> {<br>     return is_array( $array ) ? count( array_filter( array_keys( $array ), "is_numeric" ) ) : 0;<br> }

附件 IndexModel.class.zip ( 705 B 下载:101 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン