简体   繁体   English

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

[英]How to get the name and values from xml array using php

Here is my Xml code: 这是我的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
                                            )

                                    )

                            )

                    )


            )       
    )       

)

Above Xml array need to print like key and values type.The array showing like dimension and metric.I want print the values inside @attributes nested array like key and value. 在Xml数组上面需要打印像键和值类型。数组显示像维度和指标。我想在@attributes嵌套数组内打印值,如键和值。

Thanks. 谢谢。

Found in the comments on php SimpleXml documentation: 在php SimpleXml文档的注释中找到:

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

return $out;
}

Check the docs here: http://cl1.php.net/ref.simplexml 在此处检查文档: http : //cl1.php.net/ref.simplexml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM