php simplexmlelement object 数组,php中将SimpleXMLElement Object数组转化为普通数组

php中将SimpleXMLElement Object数组转化为普通数组

在PHP中可以用simplexml_load_file或者simplexml_load_string 方便地进行XML的分析,但是这两个方法返回的都是 SimpleXMLElement 用起来还是很不方便。

在支付宝接口demo里有这样的写法:

/**

* 通过节点路径返回字符串的某个节点值

* $res_data——XML 格式字符串

* 返回节点参数

*/

function getDataForXML($res_data,$node)

{

$xml = simplexml_load_string($res_data);

$result = $xml->xpath($node);

while(list( , $node) = each($result))

{

return $node;

}

}

总是觉得不怎么灵活,如果我想去所有的节点数据感觉操作一点都不方便。

其实php提供了更简单的方法,直接把 $xml 对象转换为数据就可以了

$xml = (array)$xml;

看代码 哈哈:

$_POST['notify_data'] = '150个xxxxxstefan321@qq.com2012-02-27 15:26:28trade_status_sync1xxxx2012-02-27 15:27:15xxxxTRADE_FINISHEDN0.012012-02-27 15:27:15xxxxx@gmail.com2012-02-27 15:27:140.01xxxxxxxxxxN';

$xml = simplexml_load_string($_POST['notify_data']);

$notify['payment_type'] = $xml->xpath('/notify/payment_type');

$notify['subject'] = $xml->xpath('/notify/subject');

$notify['trade_no'] = $xml->xpath('/notify/trade_no');

$notify['buyer_email'] = $xml->xpath('/notify/buyer_email');

$notify['gmt_create'] = $xml->xpath('/notify/gmt_create');

$notify['notify_type'] = $xml->xpath('/notify/notify_type');

$notify['out_trade_no'] = $xml->xpath('/notify/out_trade_no');

$notify['notify_time'] = $xml->xpath('/notify/notify_time');

$notify['seller_id'] = $xml->xpath('/notify/seller_id');

$notify['trade_status'] = $xml->xpath('/notify/trade_status');

$notify['total_fee'] = $xml->xpath('/notify/total_fee');

$notify['seller_email'] = $xml->xpath('/notify/seller_email');

$notify['price'] = $xml->xpath('/notify/price');

$notify['buyer_id'] = $xml->xpath('/notify/buyer_id');

$notify['notify_id'] = $xml->xpath('/notify/notify_id');

print_r($notify);

$xml = (array)$xml;

print_r($xml);

输出:

Array

(

[payment_type] => Array

(

[0] => SimpleXMLElement Object

(

[0] => 1

)

)

[subject] => Array

(

[0] => SimpleXMLElement Object

(

[0] => 50个xx

)

)

[trade_no] => Array

(

[0] => SimpleXMLElement Object

(

[0] => xxx

)

)

[buyer_email] => Array

(

[0] => SimpleXMLElement Object

(

[0] => stefan321@qq.com

)

)

[gmt_create] => Array

(

[0] => SimpleXMLElement Object

(

[0] => 2012-02-27 15:26:28

)

)

[notify_type] => Array

(

[0] => SimpleXMLElement Object

(

[0] => trade_status_sync

)

)

[out_trade_no] => Array

(

[0] => SimpleXMLElement Object

(

[0] => xxxx

)

)

[notify_time] => Array

(

[0] => SimpleXMLElement Object

(

[0] => 2012-02-27 15:27:15

)

)

[seller_id] => Array

(

[0] => SimpleXMLElement Object

(

[0] => xxxx

)

)

[trade_status] => Array

(

[0] => SimpleXMLElement Object

(

[0] => TRADE_FINISHED

)

)

[total_fee] => Array

(

[0] => SimpleXMLElement Object

(

[0] => 0.01

)

)

[seller_email] => Array

(

[0] => SimpleXMLElement Object

(

[0] => xxxxx@gmail.com

)

)

[price] => Array

(

[0] => SimpleXMLElement Object

(

[0] => 0.01

)

)

[buyer_id] => Array

(

[0] => SimpleXMLElement Object

(

[0] => xxxxx

)

)

[notify_id] => Array

(

[0] => SimpleXMLElement Object

(

[0] => xxxxx

)

)

)

Array

(

[payment_type] => 1

[subject] => 50个xx

[trade_no] => xxx

[buyer_email] => stefan321@qq.com

[gmt_create] => 2012-02-27 15:26:28

[notify_type] => trade_status_sync

[quantity] => 1

[out_trade_no] => xxxx

[notify_time] => 2012-02-27 15:27:15

[seller_id] => xxxx

[trade_status] => TRADE_FINISHED

[is_total_fee_adjust] => N

[total_fee] => 0.01

[gmt_payment] => 2012-02-27 15:27:15

[seller_email] => xxxxx@gmail.com

[gmt_close] => 2012-02-27 15:27:14

[price] => 0.01

[buyer_id] => xxxxx

[notify_id] => xxxxx

[use_coupon] => N

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值