设为首页 收藏本站
查看: 754|回复: 0

[经验分享] php中将SimpleXMLElement Object数组转化为普通数组

[复制链接]

尚未签到

发表于 2017-4-9 07:10:10 | 显示全部楼层 |阅读模式
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'] = '<notify><payment_type>1</payment_type><subject>50个xx</subject><trade_no>xxx</trade_no><buyer_email>stefan321@qq.com</buyer_email><gmt_create>2012-02-27 15:26:28</gmt_create><notify_type>trade_status_sync</notify_type><quantity>1</quantity><out_trade_no>xxxx</out_trade_no><notify_time>2012-02-27 15:27:15</notify_time><seller_id>xxxx</seller_id><trade_status>TRADE_FINISHED</trade_status><is_total_fee_adjust>N</is_total_fee_adjust><total_fee>0.01</total_fee><gmt_payment>2012-02-27 15:27:15</gmt_payment><seller_email>xxxxx@gmail.com</seller_email><gmt_close>2012-02-27 15:27:14</gmt_close><price>0.01</price><buyer_id>xxxxx</buyer_id><notify_id>xxxxx</notify_id><use_coupon>N</use_coupon></notify>';
$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
)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362178-1-1.html 上篇帖子: 以文本文件为基础的PHP搜索引擎 下篇帖子: 提高PHP编程效率的53个要点(转载)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表