《PHP學(xué)習(xí):php實現(xiàn)xml轉(zhuǎn)換數(shù)組的方法示例》要點:
本文介紹了PHP學(xué)習(xí):php實現(xiàn)xml轉(zhuǎn)換數(shù)組的方法示例,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP實戰(zhàn)本文實例講述了php實現(xiàn)xml轉(zhuǎn)換數(shù)組的方法.分享給大家供大家參考,具體如下:
PHP實戰(zhàn)
<?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實戰(zhàn)運行結(jié)果如下:
PHP實戰(zhàn)
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實戰(zhàn)PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
PHP實戰(zhàn)在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
PHP實戰(zhàn)在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
PHP實戰(zhàn)XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
PHP實戰(zhàn)XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
PHP實戰(zhàn)更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯誤與異常處理方法總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP實戰(zhàn)希望本文所述對大家PHP程序設(shè)計有所幫助.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/1910.html