本文目錄一覽:
PHP xml轉換成數組
xml轉array方法沒錯,只是xml中有三個list,而數組中卻不能出現三個$arr[‘list’],所以這個方法自動把三個list中的內容放進了一個二維數組中。
可以嘗試直接取$arr[‘list’],取出結果應該就是 Array ( [0] = 1 [1] = 2 [2] = 3 ) 了。
php 如何獲取XML 並轉成2維數組
?php
/**
* 功能:解析xml數據轉換成二維數組
*
* @param string $dataXml
* @return array
*/
public static function getXmlData ( $strXml ) {
$pos = strpos($strXml, ‘xml’);
if ($pos) {
$xmlCode =simplexml_load_string($strXml,’SimpleXMLElement’, LIBXML_NOCDATA);
$arrayCode=self::get_object_vars_final($xmlCode);
return $arrayCode ;
} else {
return ”;
}
}
?
在PHP中將數組轉換為XML格式
php數組格式:
Array to XML:
通過使用PHP的擴展SimpleXML,我們將uses_array轉換為xml格式。
保存成功的XML文件:
The users.xml file contains the following xml.
附註:
Insert XML Into Databse
If you want to save the XML into the database, then replace the $xml_file variable line with the following code line. Now you can insert $xml_file variable into the database.
用PHP寫代碼 讀取這段$xml變量中的 XML的節點屬性和值
PHP里有一個SimpleXML很好用的,你可以試試
比如
$doc = new SimpleXMLElement($xml);
如要調用文本節點,像這樣
echo $doc-response-result;
上面這一句將輸出PASSPORT–passport time out.
要調用屬性值的話,像這樣
echo $doc-response-result[‘code’];
上一句輸出-3
你也可以用print_r($doc);輸出,SimpleXML就是吧一個XML轉化成一個數組,你可以參考PHP文檔
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/154538.html