Home >Backend Development >PHP Tutorial >About the method of encapsulating data in xml
This article mainly introduces the method of encapsulating data in xml, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
xml encapsulation Data method
<?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>Running result: The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website! Related recommendations:
How to configure independent subdomain names in the yii backend
The above is the detailed content of About the method of encapsulating data in xml. For more information, please follow other related articles on the PHP Chinese website!