PHP 数组转XML 格式
Posted ﹏Sakura
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 数组转XML 格式相关的知识,希望对你有一定的参考价值。
function buildXml( $data, $wrap= ‘xml‘ ){ $str = "<{$wrap}>"; if( is_array( $data ) ){ if( hasIndex( $data ) ){ foreach( $data as $k=>$v ){ $str .= buildXml( $v, $k ); } }else{ foreach( $data as $v ){ foreach( $v as $k1=>$v1 ) $str .= buildXml( $v1, $k1 ); } } }else $str .= $data; $str .= "</{$wrap}>"; return $str; } function hasIndex( $arr ){ return array_keys($arr) !== range(0, count($arr) - 1); } $arr1 = [ ‘title‘=>‘title‘, ‘bbb‘=>[ ‘AAA‘=>12, ‘BBB‘=>555 ] ]; $arr2 = [ ‘aaa‘=>1, ‘bbb‘=>[ [‘AAA‘=>[ ‘BBB‘=>[ [‘DDDD‘=>111],[‘DDDD‘=>222] ] ]], [‘AAA‘=>6] ] ]; header("Content-type: text/xml"); echo ‘<?xml version="1.0" encoding="utf-8"?>‘; echo buildXml($arr2);
以上是关于PHP 数组转XML 格式的主要内容,如果未能解决你的问题,请参考以下文章