南宁琅东汽车站,怀牛膝,www.zplusz.org
1.xml方式封装数据方法
2.demo
<?php xml方式封装数据方法 /** * [xmlEncode description] * @param [type] $code [description] * @param [type] $message [description] * @param array $data [description] * @return [type] [description] */ public static function xmlEncode($code,$message,$data= array()){ if(!is_numeric($code)){ return; } $result = array( 'code'=>$code, 'message'=>$message, 'data'=>$data, ); header("Content-Type:text/html"); $xml ="<?xml version='1.0' encoding='UTF-8'>"; $xml .="<root>"; $xml .=self::xmlToEncode($result); $xml .="</root>"; echo $xml; } public static function xmlToEncode($data){ $xml = $attr ""; foreach ($data as $key => $value) { //xml的节点不能为数字,如果传默认数组需要处理下标值 if(is_numeric($key)){ $attr = "id='{$key}'"; $key = "item"; } $xml .="<{$key}>"; $xml .=is_array($value)?self::xmlToEncode($value):$value; $xml .="</{$key}>"; } return $xml; } $data = array( 'id'=>1, 'name'=>'xinlang', 'type'=>array(), ); Response::xmlEncode(200,'success',$data); //注意 xml的节点不能为数字,如果传默认数组需要处理下标值 <item id="0"></item>
运行结果:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
php的instanceof和判断闭包Closure操作示例
php数组函数array_push()、array_pop()及array_shift()简单用法示例
yii2.0框架实现上传excel文件后导入到数据库的方法示例
php使用goto实现自动重启swoole、reactphp、workerman服务的代码
网友评论