首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

xml转换成数组的有关问题

2012-04-16 
xml转换成数组的问题现有如下xml:XML coderootrowserrMsg text/retValue texttrue//rows

xml转换成数组的问题
现有如下xml:

XML code
<root><rows><errMsg text=""/><retValue text="true"/></rows><records><productno text="000321"/><billno text=""/><orderno text="D004410439"/><out_trade_no text="100001"/><plcprem text="0.0"/><orderprem text="50.0"/><commision text="0.0"/></records>....<root>

xml深度不确定但每个节点都有text属性,想转换成如下数组,有什么好办法吗
PHP code
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 =<<< XML<root><rows><errMsg text=""/><retValue text="true"/></rows><records><productno text="000321"/><billno text=""/><orderno text="D004410439"/><out_trade_no text="100001"/><plcprem text="0.0"/><orderprem text="50.0"/><commision text="0.0"/></records></root>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);
[解决办法]
探讨
PHP code
$s =<<< XML
<root>
<rows>
<errMsg text=""/>
<retValue text="true"/>
</rows>
<records>
<productno text="000321"/>
<billno text=""/>
<orderno text="D004410439"/>
<out_trade_no text="100001"/>
……

热点排行