We help IT Professionals succeed at work.

php object to array

Robert Saylor
on
88 Views
Last Modified: 2018-02-04
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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Robert SaylorSenior Developer

Author

Commented:
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
                        )
gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
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
gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
No reason for deletion?
Robert SaylorSenior Developer

Author

Commented:
no solution given and I have retracted my question.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.
Robert SaylorSenior Developer

Author

Commented:
This did work for part of the return object but due to the complex of the object I have went another direction.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.