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

时间:2014-01-09 13:03:38

标签: php xml

这是我的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数组需要像键和值类型一样打印。数组显示类似维度和度量。我想打印@attributes嵌套数组中的值,如键和值。

感谢。

1 个答案:

答案 0 :(得分:1)

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

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

return $out;
}

点击此处的文档:http://cl1.php.net/ref.simplexml