Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically access nested object

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!

like image 894
bo-oz Avatar asked Aug 12 '11 08:08

bo-oz


2 Answers

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;
like image 187
Gordon Avatar answered Nov 20 '22 20:11

Gordon


If you're not able to use xPath and need to access a object dynamically, you can use the following approach:

$oObj = new StdClass;
$oObj->Root->Parent->ID = 1;
$oObj->Root->Parent->Child->ID = 2;

$sSeachInTree = 'Root\\Parent\\Child\\ID';
$aElements = explode("\\",$sSeachInTree);

foreach($aElements as $sElement)
{
    if (isset($oObj->{$sElement}))
    {
        if (end($aElements) == $sElement)       
        {
            echo "Found: " . $sElement . " = " . $oObj->{$sElement};
        }
        $oObj = $oObj->{$sElement};
    }
}
like image 23
Nielsos Avatar answered Nov 20 '22 19:11

Nielsos