yinl_li 发表于 2017-4-9 07:10:10

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'] = '<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
(
=> Array
(
=> SimpleXMLElement Object
(
=> 1
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> 50个xx
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> xxx
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> stefan321@qq.com
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> 2012-02-27 15:26:28
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> trade_status_sync
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> xxxx
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> 2012-02-27 15:27:15
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> xxxx
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> TRADE_FINISHED
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> 0.01
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> xxxxx@gmail.com
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> 0.01
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> xxxxx
)
)
=> Array
(
=> SimpleXMLElement Object
(
=> xxxxx
)
)
)
Array
(
=> 1
=> 50个xx
=> xxx
=> stefan321@qq.com
=> 2012-02-27 15:26:28
=> trade_status_sync
=> 1
=> xxxx
=> 2012-02-27 15:27:15
=> xxxx
=> TRADE_FINISHED
=> N
=> 0.01
=> 2012-02-27 15:27:15
=> xxxxx@gmail.com
=> 2012-02-27 15:27:14
=> 0.01
=> xxxxx
=> xxxxx
=> N
)
页: [1]
查看完整版本: php中将SimpleXMLElement Object数组转化为普通数组