//数组转xml function arr2xml($arr,$node=null){ if($node == null){ $sim = new SimpleXmlElement(‘<?xml version="1.0" encoding="utf8"?><root></root>‘); }else{ $sim = $node; } foreach($arr as $k=>$v){ if(!is_numeric($k) && is_array($v)){ // echo ‘1‘; arr2xml($v,$sim->addChild($k)); }elseif(is_numeric($k) && !is_array($v)){ // echo ‘2‘; $sim->addChild(‘item‘,$v); }elseif(is_numeric($k) && is_array($v)){ // echo ‘3‘; arr2xml($v,$sim->addChild(‘item‘)); }elseif(!is_numeric($k) && !is_array($v)){ // echo ‘4‘; $sim->addChild($k,$v); } } return $sim->savexml(); } //测试方法 $data = array( ‘name‘=>‘xiaobai‘, ‘age‘=>25, ‘我是索引元素‘, array(‘ceshi‘=>‘ssss‘), ‘company‘=>array(‘cfo‘=>‘xiaoming‘,‘ceo‘=>‘xiaohong‘,‘it‘=>array(‘laoli‘,‘laowang‘)) ); header(‘content-type:text/xml‘); echo arr2xml($data);
本文出自 “手掌上的阳光” 博客,请务必保留此出处http://xiaobai123.blog.51cto.com/11280470/1958073
原文地址:http://xiaobai123.blog.51cto.com/11280470/1958073