动态访问嵌套对象 [英] Dynamically access nested object

查看:99
本文介绍了动态访问嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个可以利用多种Web服务进行地理编码的Geocoding类(即Google,Yahoo,Bing等)。我试图用这种方式来实现新的web服务可以很容易地配置。大多数Web服务返回XML / JSON ..对于PHP,我选择XML作为我的主要焦点。所有的代码已经存在,但现在谷歌返回下面的XML(转换为simple_xml_element)

  SimpleXMLElement Object 

[status] => OK
[result] => Array

[0] => SimpleXMLElement Object

[type] => postal_code
[formatted_address] => 1010瑞士洛桑
[address_component] => Array

[0] => SimpleXMLElement对象

[long_name] => 1010
[短名称] => 1010
[类型] =>邮政编码


[1] => SimpleXMLElement对象

[long_name] =>洛桑
[short_name] =>洛桑
[type] => Array

[0] => locality
[1] =>政治




[2] => SimpleXMLElement Object

[long_name] => Vaud
[short_name] => VD
[type] => Array

[ 0] => administrative_area_level_1
[1] =>政治




[3] => SimpleXMLElement Object

[long_name] => Switzerland
[short_name] => CH
[type] => Array

[ 0] => country
[1] =>政治






[geometry ] => SimpleXMLElement对象

[位置] => SimpleXMLElement对象

[lat] => 46.5376186
[lng] => 6.6539665


[location_type] => APPROXIMATE
[viewport] => SimpleXMLElement Object

[southwest] => SimpleXMLElement Object

[lat] => 46.5253574
[lng] => 6.6384420


[northeast] => SimpleXMLElement对象

[lat] => 46.5467887
[lng] => 6.6745222




[bounds] => SimpleXMLElement对象

[southwest] => SimpleXMLElement对象

[lat] => 46.5253574
[lng] => 6.6384420


[northeast] => SimpleXMLElement对象

[lat] => 46.5467887
[lng] => 6.6745222
)$ b b b b b b b b b b b b b b b b b b b b b b c $ c

我需要的信息位于[location]标记中,因此我试图将路径存储在var中:

  $ lat_path ='result [0]  - >> geometry-> location-> lat; 

然后尝试以这种方式访问​​该值:

 (假设$ xml是对象)
$ xml-> {$ lat_path};

但这并不奏效。有没有什么办法可以基于动态或变量来访问信息。我不想用Google特定的代码破坏我的Geocoding方法。



谢谢!

解决方案

当你做的时候

  $ xml-> {$ lat_path}; 

PHP将使用 $ lat_path 中的任何内容作为变量名称。它不会进入对象图或完全服从 T_OBJECT_OPERATOR 。它只会寻找一个属性

 'result [0]  - > geometry-> location-> lat;' 

位于 $ xml 中。尝试运行以下代码作为示例:

  $ obj = new StdClass; 
$ obj-> {'result [0] - > geometry-> location-> lat;'} = 1;
print_r($ obj);

它会输出

  stdClass Object 

[result [0] - > geometry-> location-> lat;] => 1

正如您所看到的,它是一个单独的属性,而不是嵌套的对象图。



像评论中提示的一样,可以使用XPath或直接转到期望值:

  $ xml- >导致[0]  - >几何性质 - >位置 - > LAT; 


I'm building a Geocoding class that can utilize multiple webservices for Geocoding (ie Google, Yahoo, Bing, etc.). I'm trying to make it in a such a way that new webservices can be easily configured. Most of the webservices return either XML/JSON.. for PHP I chose XML as my primary focus. All the code is already in place, but now Google for instance returns the following XML (transformed to a simple_xml_element)

SimpleXMLElement Object
 (
[status] => OK
[result] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [type] => postal_code
                [formatted_address] => 1010 Lausanne, Switzerland
                [address_component] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [long_name] => 1010
                                [short_name] => 1010
                                [type] => postal_code
                            )

                        [1] => SimpleXMLElement Object
                            (
                                [long_name] => Lausanne
                                [short_name] => Lausanne
                                [type] => Array
                                    (
                                        [0] => locality
                                        [1] => political
                                    )

                            )

                        [2] => SimpleXMLElement Object
                            (
                                [long_name] => Vaud
                                [short_name] => VD
                                [type] => Array
                                    (
                                        [0] => administrative_area_level_1
                                        [1] => political
                                    )

                            )

                        [3] => SimpleXMLElement Object
                            (
                                [long_name] => Switzerland
                                [short_name] => CH
                                [type] => Array
                                    (
                                        [0] => country
                                        [1] => political
                                    )

                            )

                    )

                [geometry] => SimpleXMLElement Object
                    (
                        [location] => SimpleXMLElement Object
                            (
                                [lat] => 46.5376186
                                [lng] => 6.6539665
                            )

                        [location_type] => APPROXIMATE
                        [viewport] => SimpleXMLElement Object
                            (
                                [southwest] => SimpleXMLElement Object
                                    (
                                        [lat] => 46.5253574
                                        [lng] => 6.6384420
                                    )

                                [northeast] => SimpleXMLElement Object
                                    (
                                        [lat] => 46.5467887
                                        [lng] => 6.6745222
                                    )

                            )

                        [bounds] => SimpleXMLElement Object
                            (
                                [southwest] => SimpleXMLElement Object
                                    (
                                        [lat] => 46.5253574
                                        [lng] => 6.6384420
                                    )

                                [northeast] => SimpleXMLElement Object
                                    (
                                        [lat] => 46.5467887
                                        [lng] => 6.6745222
                                    )

                            )

                    )

            )
)

The information I need is in the [location] tag, so I've tried storing the path in a var:

$lat_path = 'result[0]->geometry->location->lat;

And then try to access the value this way:

(suppose $xml is the object)
$xml->{$lat_path};

But this doens't work. Is there any way I can access the information dynamically or variable based. I do not want to ruin my Geocoding method with Google specific code.

Thanks!

解决方案

When you do

$xml->{$lat_path};

PHP will use anything within $lat_path as the variable name. It will not go into the object graph or obey the T_OBJECT_OPERATOR at all. It will simply look for a property

 'result[0]->geometry->location->lat;'

in $xml. Try to run this code for an example:

$obj = new StdClass;
$obj->{'result[0]->geometry->location->lat;'} = 1;
print_r($obj);

It will output

stdClass Object
(
    [result[0]->geometry->location->lat;] => 1
)

As you can see, it is one single property, not a nested object graph.

Like suggested in the comments, either use XPath or go to the desired value directly:

$xml->result[0]->geometry->location->lat;

这篇关于动态访问嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆