`

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

    博客分类:
  • PHP
 
阅读更多
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
)
0
0
分享到:
评论
2 楼 idong杨 2015-08-05  
[url][url][url][url][url][/url][/url][/url][/url][/url]
1 楼 idong杨 2015-08-05  

相关推荐

Global site tag (gtag.js) - Google Analytics