2017-06-22 70 views

回答

4

您可以使用array_filter過濾address_components場爆炸每個types

<?php 

$apiKey = "API_KEY"; 

$url = "https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=".$apiKey; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL,$url); 
$result=curl_exec($ch); 
curl_close($ch); 

$data = json_decode($result,true); 

$components = $data["results"][0]["address_components"]; 

// filter the address_components field for type : $type 
function filter($components, $type) 
{ 
    return array_filter($components, function($component) use ($type) { 
     return array_filter($component["types"], function($data) use ($type) { 
      return $data == $type; 
     }); 
    }); 
} 

$zipcode = array_values(filter($components, "postal_code"))[0]["long_name"]; 
$citystate = array_values(filter($components, "administrative_area_level_1"))[0]["long_name"]; 

var_dump($zipcode); 
var_dump($citystate); 

?>