Access json data, can't get to a value

Hello everyone!

I am trying to get some value from a query to the Google maps reverse geolocation API and I get something like in the following link

Data gotten from Google Maps API

from there I can get some information using this


foreach ( $data->results as $result ) {
	foreach ( $result as $res ) {
		if(is_array($res)){
			if(is_object($res[0])){
				switch ($res[0]->types[0]){
					case "street_number":
						$street_number = $res[0]->long_name;
					break;
					case "route":
						$route = $res[0]->long_name;
					break;
					case "neighborhood":
						$neighborhood = $res[0]->long_name;
					break;
					case "locality":
						$city = $res[0]->long_name;
					break;
					case "administrative_area_level_1":
						$state = $res[0]->long_name;
					break;
					case "postal_code":
						$postal_code = $res[0]->long_name;
					break;
					case "country":
						$country = $res[0]->long_name;
					break;					
				}
			}
		}
	}
}
echo '{"country":"' . $country . '","city":"' . $city . '","postal_code":"' . $postal_code . '","street_number":"' . $street_number . '","route":"' . $route . '","neighborhood":"' . $neighborhood . '","state":"' . $state . '","url":"' . $url . '"}';

For that address provided this is the data I get in response after the PHP script


{
"country":"Mexico",
"city":"",
"postal_code":"",
"street_number":"",
"route":"Sin Número No. 3",
"neighborhood":"",
"state":"Oaxaca",
"url":"https://maps.googleapis.com/maps/api/geocode/json?latlng=17.185264431680135,-96.76821982574461&sensor=true"
}

I am trying to get the values of the address like street, street number, postal code, state country etc and that does work but for some reason it will only get some data, the structure of the data is the same for each value so I was under the assumption I could get every detail the same way but as you can see the city, street number, postal code and neighborhood are missing, from all those values if you look at the link I posted above street number and postal code are missing so that is a correct response but city and neighborhood are there and I am not getting them, can someone tell me what I’m doing wrong!

Okay well, I finally got it, this is the way I did it


for ( $i = 0; $i < count ( $data->results[0]->address_components ); $i ++ ) {
	switch ( $data->results[0]->address_components[$i]->types[0] ) {
		case "street_number":
			$street_number = $data->results[0]->address_components[$i]->long_name;
		break;
		case "route":
			$route = $data->results[0]->address_components[$i]->long_name;
		break;
		case "neighborhood":
			$neighborhood = $data->results[0]->address_components[$i]->long_name;
		break;
		case "locality":
			$city = $data->results[0]->address_components[$i]->long_name;
		break;
		case "administrative_area_level_1":
			$state = $data->results[0]->address_components[$i]->long_name;
		break;
		case "postal_code":
			$postal_code = $data->results[0]->address_components[$i]->long_name;
		break;
		case "country":
			$country = $data->results[0]->address_components[$i]->long_name;
		break;
	}
}