search
Homephp教程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元

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software