Jump to content

stdClass Object Multi Dimensional Array


savagenoob

Recommended Posts

I have a stdClass Object Multi Dimensional Array that I need to dissassemble into variables but dont know how to pull the data. The array looks like this:

stdClass Object ( [results] => Array ( [0] => stdClass Object ( [address_components] => Array ( [0] => stdClass Object ( [long_name] => 1600 [short_name] => 1600 [types] => Array ( [0] => street_number ) ) [1] => stdClass Object ( [long_name] => Pennsylvania Ave NW [short_name] => Pennsylvania Ave NW [types] => Array ( [0] => route ) ) [2] => stdClass Object ( [long_name] => Northwest Washington [short_name] => Northwest Washington [types] => Array ( [0] => neighborhood [1] => political ) ) [3] => stdClass Object ( [long_name] => Washington [short_name] => Washington [types] => Array ( [0] => locality [1] => political ) ) [4] => stdClass Object ( [long_name] => District of Columbia [short_name] => DC [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [5] => stdClass Object ( [long_name] => United States [short_name] => US [types] => Array ( [0] => country [1] => political ) ) [6] => stdClass Object ( [long_name] => 20500 [short_name] => 20500 [types] => Array ( [0] => postal_code ) ) ) [formatted_address] => 1600 Pennsylvania Ave NW, Washington, DC 20500, USA [geometry] => stdClass Object ( [location] => stdClass Object ( [lat] => 38.8987149 [lng] => -77.0376555 ) [location_type] => ROOFTOP [viewport] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 38.900063880291 [lng] => -77.036306519708 ) [southwest] => stdClass Object ( [lat] => 38.897365919709 [lng] => -77.039004480291 ) ) ) [partial_match] => 1 [types] => Array ( [0] => street_address ) ) ) [status] => OK )

Really I need to start with pulling the lat and lng from the location. No clue how to get to this data.

Link to comment
https://forums.phpfreaks.com/topic/256020-stdclass-object-multi-dimensional-array/
Share on other sites

The location would be...

$obj->results[0]->geometry->location

 

Which would contain

stdClass Object (
[lat] => 38.8987149 
[lng] => -77.0376555
)

 

Also, here's a formatted version of that mess you posted. Maybe it will help you see the flow of logic easier.

stdClass Object ( 
[results] => Array ( 
	[0] => stdClass Object ( 
		[address_components] => Array ( 
			[0] => stdClass Object ( 
				[long_name] => 1600
				[short_name] => 1600
				[types] => Array ( 
					[0] => street_number
				)
			)
			[1] => stdClass Object (
				[long_name] => Pennsylvania Ave NW
				[short_name] => Pennsylvania Ave NW
				[types] => Array (
					[0] => route
				)
			)
			[2] => stdClass Object (
				[long_name] => Northwest Washington
				[short_name] => Northwest Washington
				[types] => Array (
					[0] => neighborhood
					[1] => political
				)
			)
			[3] => stdClass Object (
				[long_name] => Washington
				[short_name] => Washington
				[types] => Array (
					[0] => locality
					[1] => political
				)
			)
			[4] => stdClass Object (
				[long_name] => District of Columbia
				[short_name] => DC
				[types] => Array (
					[0] => administrative_area_level_1
					[1] => political
				)
			)
			[5] => stdClass Object (
				[long_name] => United States
				[short_name] => US
				[types] => Array (
					[0] => country
					[1] => political
				)
			)
			[6] => stdClass Object (
				[long_name] => 20500
				[short_name] => 20500
				[types] => Array (
					[0] => postal_code
				)
			)
		)
		[formatted_address] => 1600 Pennsylvania Ave NW, Washington, DC 20500, USA
		[geometry] => stdClass Object (
			[location] => stdClass Object (
				[lat] => 38.8987149 
				[lng] => -77.0376555
			)
			[location_type] => ROOFTOP
			[viewport] => stdClass Object (
				[northeast] => stdClass Object (
					[lat] => 38.900063880291
					[lng] => -77.036306519708
				)
				[southwest] => stdClass Object (
					[lat] => 38.897365919709
					[lng] => -77.039004480291
				)
			)
		)
		[partial_match] => 1
		[types] => Array (
			[0] => street_address
		)
	)
)
[status] => OK
)

Hello,

You can try following code.

replace $obj variable with your one.

 

$obj = $test;

foreach($obj->results as $key => $val)

{

echo "Latitude: ".$key->geometry->location->lat;

echo "Longitude: ".$key->geometry->location->lng;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.