十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章給大家介紹利用php怎么將XML和數(shù)組進行相互轉(zhuǎn)化,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
數(shù)組轉(zhuǎn)化為xml:
function arrtoxml($arr,$dom=0,$item=0){ if (!$dom){ $dom = new DOMDocument("1.0"); } if(!$item){ $item = $dom->createElement("root"); $dom->appendChild($item); } foreach ($arr as $key=>$val){ $itemx = $dom->createElement(is_string($key)?$key:"item"); $item->appendChild($itemx); if (!is_array($val)){ $text = $dom->createTextNode($val); $itemx->appendChild($text); }else { arrtoxml($val,$dom,$itemx); } } return $dom->saveXML(); }
xml轉(zhuǎn)化為數(shù)組:
function xmltoarr($path){ $xmlfile = file_get_contents($path);//提取xml文檔中的內(nèi)容以字符串格式賦給變量 $ob= simplexml_load_string($xmlfile);//將字符串轉(zhuǎn)化為變量 $json = json_encode($ob);//將對象轉(zhuǎn)化為JSON格式的字符串 $configData = json_decode($json, true);//將JSON格式的字符串轉(zhuǎn)化為數(shù)組 print_r($configData); }
關(guān)于利用php怎么將XML和數(shù)組進行相互轉(zhuǎn)化就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。