PHP 数组转XML函数如下
[PHP] 纯文本查看 复制代码
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php function arrayToXml( $arr , $dom =0, $item =0){ if (! $dom ){ $dom = new DOMDocument( "1.0" ); } if (! $item ){ $item = $dom ->createElement( "root" ); $dom ->appendChild( $item ); } foreach ( $arr as $key => $val ){ $itemx = $dom ->createElement( is_string ( $key )? $key : "item" ); $item ->appendChild( $itemx ); if (! is_array ( $val )){ $text = $dom ->createTextNode( $val ); $itemx ->appendChild( $text ); } else { arrayToXml( $val , $dom , $itemx ); } } return $dom ->saveXML(); } //下面这句可以解决标题中提到的所有问题 header( 'Content-Type:text/xml' ); $test = [ 'test' =>[ 'test2' => 'test3' ] ]; echo (arrayToXml( $test )); ?> |
header('Content-Type:text/xml');
这句话的意思是告诉浏览器接下来的内容 是xml.按xml的格式去解析..没毛病.