php 数组转xml与xml转换数组实例
本文章来给各位同学介绍两个简单的实例,php 数组转xml与xml转换数组,希望此文章对各位朋友会有所帮助。
php 数组转xml,代码如下:
<?php function array2xml($array, $xml = false){ if($xml === false){ $xml = new SimpleXMLElement('<root/>'); } foreach($array as $key => $value){ if(is_array($value)){ array2xml($value, $xml->addChild($key)); }else{ $xml->addChild($key, $value); } } return $xml->asXML(); } header('Content-type: text/xml'); print array2xml($array); ?>
php xml转数组,代码如下:
<?php $s = <<<EOS <root> <Formula> <formulaname>Basic</formulaname> <movespeed>1</movespeed> <box>4</box> <chicken>3</chicken> <ducks>1</ducks> <cereal>2</cereal> </Formula> </root> EOS; $a = json_decode(json_encode((array) simplexml_load_string($s)),1); print_r($a); ?>
本文地址:http://www.phprm.com/shuzu/fs1588.html
转载随意,但请附上文章地址:-)