Avatar of Robert Saylor
Robert SaylorFlag for United States of America asked on

php object to array

How to convert this object to array?

I am using geocoder-php/google-maps-provider php-http/guzzle6-adapter php-http/message to convert the city, state to lat/long. The result is shown below when I output with print_r

Need some help converting this to an array or walk me through the steps to collect the data from the result.

Geocoder\Model\AddressCollection Object
(
    [locations:Geocoder\Model\AddressCollection:private] => Array
        (
            [0] => Geocoder\Provider\GoogleMaps\Model\GoogleAddress Object
                (
                    [id:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => ChIJXYHEcBpN-IgRW6TZSPQKM7U
                    [locationType:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => APPROXIMATE
                    [resultType:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => Array
                        (
                            [0] => locality
                            [1] => political
                        )

                    [formattedAddress:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => Aiken, SC, USA
                    [streetAddress:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [intersection:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [political:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => United States
                    [colloquialArea:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [ward:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [neighborhood:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [premise:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [subpremise:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [naturalFeature:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [airport:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [park:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [pointOfInterest:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [establishment:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => 
                    [subLocalityLevels:Geocoder\Provider\GoogleMaps\Model\GoogleAddress:private] => Geocoder\Model\AdminLevelCollection Object
                        (
                            [adminLevels:Geocoder\Model\AdminLevelCollection:private] => Array
                                (
                                )

                        )

                    [coordinates:Geocoder\Model\Address:private] => Geocoder\Model\Coordinates Object
                        (
                            [latitude:Geocoder\Model\Coordinates:private] => 33.5604168
                            [longitude:Geocoder\Model\Coordinates:private] => -81.7195533
                        )

                    [bounds:Geocoder\Model\Address:private] => Geocoder\Model\Bounds Object
                        (
                            [south:Geocoder\Model\Bounds:private] => 33.471154
                            [west:Geocoder\Model\Bounds:private] => -81.7790329
                            [north:Geocoder\Model\Bounds:private] => 33.590592
                            [east:Geocoder\Model\Bounds:private] => -81.6720859
                        )

                    [streetNumber:Geocoder\Model\Address:private] => 
                    [streetName:Geocoder\Model\Address:private] => 
                    [subLocality:Geocoder\Model\Address:private] => 
                    [locality:Geocoder\Model\Address:private] => Aiken
                    [postalCode:Geocoder\Model\Address:private] => 
                    [adminLevels:Geocoder\Model\Address:private] => Geocoder\Model\AdminLevelCollection Object
                        (
                            [adminLevels:Geocoder\Model\AdminLevelCollection:private] => Array
                                (
                                    [1] => Geocoder\Model\AdminLevel Object
                                        (
                                            [level:Geocoder\Model\AdminLevel:private] => 1
                                            [name:Geocoder\Model\AdminLevel:private] => South Carolina
                                            [code:Geocoder\Model\AdminLevel:private] => SC
                                        )

                                    [2] => Geocoder\Model\AdminLevel Object
                                        (
                                            [level:Geocoder\Model\AdminLevel:private] => 2
                                            [name:Geocoder\Model\AdminLevel:private] => Aiken County
                                            [code:Geocoder\Model\AdminLevel:private] => Aiken County
                                        )

                                )

                        )

                    [country:Geocoder\Model\Address:private] => Geocoder\Model\Country Object
                        (
                            [name:Geocoder\Model\Country:private] => United States
                            [code:Geocoder\Model\Country:private] => US
                        )

                    [timezone:Geocoder\Model\Address:private] => 
                    [providedBy:Geocoder\Model\Address:private] => google_maps
                )

        )

)

Open in new window

PHP

Avatar of undefined
Last Comment
Robert Saylor

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Robert Saylor

This is what I want to extract. I have used stdClass before but this one if much different then I am use to.

                    [coordinates:Geocoder\Model\Address:private] => Geocoder\Model\Coordinates Object
                        (
                            [latitude:Geocoder\Model\Coordinates:private] => 33.5604168
                            [longitude:Geocoder\Model\Coordinates:private] => -81.7195533
                        )
gr8gonzo

I don't understand why you're trying to convert it to an array. The data already exists in your object. Are you unsure of how to access the data or are there specific fields that you're trying to get to?

If you're unsure of how to work with objects in PHP, check out my intro article on it:

https://www.experts-exchange.com/articles/2626/Beginning-Object-Oriented-Programming-in-PHP.html
gr8gonzo

No reason for deletion?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
Robert Saylor

no solution given and I have retracted my question.
Julian Hansen

Technically a solution was given. The question was how to convert a PHP object to an array - the answer is cast it to an array with (array).

In answering the question, though, we tried to determine what the use case was as the above was not really necessary given the data was available in the object.
ASKER
Robert Saylor

This did work for part of the return object but due to the complex of the object I have went another direction.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.