如何使用php从xml数组中获取名称和值

如何使用php从xml数组中获取名称和值,php,xml,Php,Xml,以下是我的Xml代码: SimpleXMLElement Object ( [resultsRows] => SimpleXMLElement Object ( [row] => Array ( [0] => SimpleXMLElement Object ( [dimension] => Array

以下是我的Xml代码:

SimpleXMLElement Object
 (
 [resultsRows] => SimpleXMLElement Object
    (
        [row] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [dimension] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => date
                                                [value] => 20140102
                                                [label] => Date
                                            )

                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => browserType
                                                [value] => Chrome
                                                [label] => Browser Type
                                            )

                                    )

                            )

                        [metric] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => visitDuration
                                                [value] => 1242
                                                [label] => Avg. Visit Duration
                                            )

                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => bounces
                                                [value] => 3
                                                [label] => Bounces
                                            )

                                    )

                                [2] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => repeatVisitors
                                                [value] => 0
                                                [label] => Repeat Visitors
                                            )

                                    )

                                [3] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => newVisitors
                                                [value] => 5
                                                [label] => New Visitors
                                            )

                                    )

                                [4] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => visits
                                                [value] => 10
                                                [label] => Visits
                                            )

                                    )

                                [5] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => pageViews
                                                [value] => 66
                                                [label] => Page Views
                                            )

                                    )

                            )

                    )


            )       
    )       
)

上面的Xml数组需要像key和values类型一样打印。数组显示像dimension和metric。我想要像key和value一样打印@attributes嵌套数组中的值


谢谢。

可以在php SimpleXml文档的评论中找到:

function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
    $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;

return $out;
}

检查此处的文档:

看起来像xml,而不是json