PHP中怎樣把JSON文件轉為XML格式?針對這個問題,今天小編總結這篇有關格式的文章,希望幫助更多正在學習PHP的同學。

首先獲取JSON文件中的字符;
$content = file_get_contents('./data.json');然后使用函數“json_decode()”函數將字符串轉為數組;
$content = file_get_contents('./data.json');
$data = json_decode($content, true);再將數組循環拼接成XML數據;
$content = file_get_contents('./data.json');
$data = json_decode($content, true);
function xml_encode($data)
{
$string="";
foreach($data as $k => $v){
$string .= "<" . $k . ">";
//判斷是否是數組,或者,對像
if(is_array($v) || is_object($v)){
//是數組或者對像就的遞歸調用
$string .= xml_encode($v);
}else{
//取得標簽數據
$string .=$v;
}
$string .= "</" . $k . ">";
}
return $string;
}
$content = xml_encode($data);最后將XML數據寫入文件中并將后綴名改為“xml”即可。
$content = file_get_contents('./data.json');
$data = json_decode($content, true);
function xml_encode($data)
{
$string="";
foreach($data as $k => $v){
$string .= "<" . $k . ">";
//判斷是否是數組,或者,對像
if(is_array($v) || is_object($v)){
//是數組或者對像就的遞歸調用
$string .= xml_encode($v);
}else{
//取得標簽數據
$string .=$v;
}
$string .= "</" . $k . ">";
}
return $string;
}
$content = xml_encode($data);
file_put_contents('./data.xml', $content);看完上述內容,你們掌握將JSON文件轉為XML格式的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
文章標題:PHP中怎樣把JSON文件轉為XML格式?-創新互聯
鏈接URL:http://www.yijiale78.com/article48/ceechp.html
成都網站建設公司_創新互聯,為您提供網站排名、用戶體驗、電子商務、定制網站、網站導航、響應式網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯