php 字符串转换xml,php 数组转xml字符串、xml字符串转数组方法案例
數組轉xml字符串方法:兩種方法/**
*?數組轉xml字符串方法1
*?@param??arary?$data?數組數據
*?@param??boolean?$type?是否需要根節
*/
function?arrayToXml($data,?$type=true){
if(!is_array($data)?||?count($data)?<=?0)?return?false;
$xml?=?'';
if($type)?$xml?.=?'';
foreach($data?as?$item?=>?$one){
if(is_array($one)){
$child?=?arrayToXml($one,?false);
$xml?.=?"$child$item>";
}else{
if(is_numeric($one)){
$xml?.=?"$one$item>";
}else{
$xml?.=?"$item>";
}
}
}
if($type)?$xml?.=?"";
return?$xml;
}
/**
*?數組轉xml字符串方法2
*?@param??arary?$data?數組數據
*?@param??boolean?$type?是否需要根節
*/
function?arrayToXml($data,?$type=true){
if(!is_array($data)?||?count($data)?<=?0)?return?false;
$xml?=?'';
$fn?=?function?($data,?$type)?use(&$fn){
$str?=?'';
if($type)?$str?.=?'';
foreach?($data?as?$item?=>?$one)?{
if(is_array($one)){
$do_str?=?$fn($one,?false);
$str?.=?"$do_str$item>";
}else{
if(is_numeric($one)){
$str?.=?"$one$item>";
}else{
$str?.=?"$item>";
}
}
}
if($type)?$str?.=?'';
return?$str;
};
$xml?.=?$fn($data,?$type);
return?$xml;
}
xml字符串轉數組方法:/**
*?xml字符串轉數組
*?@param??string???$xml?xml字符串
*/
function?xmlToArray($xml){
$obj?=?simplexml_load_string($xml,'SimpleXMLElement',?LIBXML_NOCDATA);
$json?=?json_encode($obj);
$data?=?json_decode($json,?true);
return?$data;
}
瀏覽器啟用彈出窗口過濾功能,將無法跳轉到下載頁。在瀏覽器地址欄右邊符號提示處點擊允許就可以了!
鄭重聲明:
1、本站源碼僅供個人學習研究和交流使用,請于下載后二十四小時內刪除
2、本站大多資源來源于互聯網、用戶分享,僅供學習交流使用,本站不提供任何技術支持
3、本站聯系方式Email:admin@youhutong.com ,收到郵件會第一時間處理。
4、如侵犯到任何版權問題,請立即告知本站(立即在線告知),本站將及時刪除并致以最深的歉意
總結
以上是生活随笔為你收集整理的php 字符串转换xml,php 数组转xml字符串、xml字符串转数组方法案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java ing印版,Java中有趣的事
- 下一篇: 直流电动机matlab仿真实验,直流电动