xml转换成数组的有关问题
Jun 13, 2016 pm 01:37 PM
gt
lt
quot
records
text
xml转换成数组的问题
现有如下xml:
- XML code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <root> <rows> <errmsg text=""></errmsg> <retvalue text="true"></retvalue> </rows> <records> <productno text="000321"></productno> <billno text=""></billno> <orderno text="D004410439"></orderno> <out_trade_no text="100001"></out_trade_no> <plcprem text="0.0"></plcprem> <orderprem text="50.0"></orderprem> <commision text="0.0"></commision> </records> .... <root> </root></root>
登入後複製
xml深度不确定但每个节点都有text属性,想转换成如下数组,有什么好办法吗
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> Array ( [rows] => Array ( [errMsg] => [retValue] => true ) [records] => Array ( [productno] =>000321 [billno] => [orderno] =>D004410439 [out_trade_no] => 100001 [plcprem] =>0.0 [orderprem] =>50.0 [commision] =>0.0 ) ..... )
登入後複製
------解决方案--------------------
看看这个
http://weblog.thomassmart.com/2008/09/php-function-xml2array/
------解决方案--------------------
- PHP code
$s = <rows> <errmsg text=""></errmsg> <retvalue text="true"></retvalue> </rows> <records> <productno text="000321"></productno> <billno text=""></billno> <orderno text="D004410439"></orderno> <out_trade_no text="100001"></out_trade_no> <plcprem text="0.0"></plcprem> <orderprem text="50.0"></orderprem> <commision text="0.0"></commision> </records> XML; $obj = simplexml_load_string($s); $r = array(); foreach($obj as $name=>$nodes) { foreach($nodes as $k=>$v) { $t = (array)$v->attributes()->text; $r[$name][$k] = $t[0]; } } print_r($r); <br><font color="#e78608">------解决方案--------------------</font><br>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Microsoft正在推出Windows 11 23H2版本到有Copilot的發布預覽頻道
