douhuo1738 2014-01-09 13:03
浏览 50

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

Here is my Xml code:

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.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongwen5870 2014-01-09 13:14
    关注

    Found in the comments on php SimpleXml documentation:

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目