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个xxxxx[email protected]2012-02-27 15:26:28trade_status_sync1xxxx2012-02-27 15:27:15xxxxTRADE_FINISHEDN0.012012-02-27 15:27:15[email protected]2012-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] => [email protected]
                )

        )

    [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] => [email protected]
                )

        )

    [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] => [email protected]
    [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] => [email protected]
    [gmt_close] => 2012-02-27 15:27:14
    [price] => 0.01
    [buyer_id] => xxxxx
    [notify_id] => xxxxx
    [use_coupon] => N
)

你可能感兴趣的:(php中将SimpleXMLElement Object数组转化为普通数组)