《PHP學習:php實現xml轉換數組的方法示例》要點:
本文介紹了PHP學習:php實現xml轉換數組的方法示例,希望對您有用。如果有疑問,可以聯系我們。
PHP實戰本文實例講述了php實現xml轉換數組的方法.分享給大家供大家參考,具體如下:
PHP實戰
<?php
$info = '<?xml version="1.0" encoding="utf-8" ?>
<data>
<GeocoderSearchResponse>
<status>OK</status>
<result>
<location>
<lat>39.94921</lat>
<lng>116.463619</lng>
</location>
<precise>0</precise>
<confidence>50</confidence>
<level>腳本</level>
</result>
</GeocoderSearchResponse>
<GeocoderSearchResponse>
<status>OK</status>
<result>
<location>
<lat>39</lat>
<lng>116</lng>
</location>
<precise>0</precise>
<confidence>50</confidence>
<level>腳本123</level>
</result>
</GeocoderSearchResponse>
</data>';
$xml = simplexml_load_string($info);
function xml2array($xmlobject) {
if ($xmlobject) {
foreach ((array)$xmlobject as $k=>$v) {
$data[$k] = !is_string($v) ? xml2array($v) : $v;
}
return $data;
}
}
$data = xml2array($xml);
var_dump($data);
?>
PHP實戰運行結果如下:
PHP實戰
array(1) {
["GeocoderSearchResponse"]=>
array(2) {
[0]=>
array(2) {
["status"]=>
string(2) "OK"
["result"]=>
array(4) {
["location"]=>
array(2) {
["lat"]=>
string(8) "39.94921"
["lng"]=>
string(10) "116.463619"
}
["precise"]=>
string(1) "0"
["confidence"]=>
string(2) "50"
["level"]=>
string(6) "腳本"
}
}
[1]=>
array(2) {
["status"]=>
string(2) "OK"
["result"]=>
array(4) {
["location"]=>
array(2) {
["lat"]=>
string(2) "39"
["lng"]=>
string(3) "116"
}
["precise"]=>
string(1) "0"
["confidence"]=>
string(2) "50"
["level"]=>
string(9) "腳本123"
}
}
}
}
PHP實戰PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
PHP實戰在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson
PHP實戰在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
PHP實戰XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
PHP實戰XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
PHP實戰更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP錯誤與異常處理方法總結》、《PHP運算與運算符用法總結》、《PHP網絡編程技巧總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP實戰希望本文所述對大家PHP程序設計有所幫助.