2012-05-06 10 views
0

Google Geocode v3 API XMLレスポンスから郵便番号を抽出しようとしています。私が抱えている困難は、時にはpostal_codeの値が6番目のaddress_elementに、時には7番目に含まれていることです。ここで私はあなたがPOSTAL_CODEがaddress_component配列の6番目の要素であることがわかりますとして、このサンプル応答で働いていますXMLレスポンスです:SimpleXMLElementを繰り返し、郵便番号値を取得します。

SimpleXMLElement Object 
(
[status] => OK 
[result] => Array 
    (
     [0] => SimpleXMLElement Object 
      (
       [type] => street_address 
       [formatted_address] => 177 Church St, Toronto, ON M5C 2G5, Canada 
       [address_component] => Array 
        (
         [0] => SimpleXMLElement Object 
          (
           [long_name] => 177 
           [short_name] => 177 
           [type] => street_number 
          ) 

         [1] => SimpleXMLElement Object 
          (
           [long_name] => Church St 
           [short_name] => Church St 
           [type] => route 
          ) 

         [2] => SimpleXMLElement Object 
          (
           [long_name] => Toronto 
           [short_name] => Toronto 
           [type] => Array 
            (
             [0] => locality 
             [1] => political 
            ) 

          ) 

         [3] => SimpleXMLElement Object 
          (
           [long_name] => Toronto 
           [short_name] => Toronto 
           [type] => Array 
            (
             [0] => administrative_area_level_2 
             [1] => political 
            ) 

          ) 

         [4] => SimpleXMLElement Object 
          (
           [long_name] => Ontario 
           [short_name] => ON 
           [type] => Array 
            (
             [0] => administrative_area_level_1 
             [1] => political 
            ) 

          ) 

         [5] => SimpleXMLElement Object 
          (
           [long_name] => Canada 
           [short_name] => CA 
           [type] => Array 
            (
             [0] => country 
             [1] => political 
            ) 

          ) 

         [6] => SimpleXMLElement Object 
          (
           [long_name] => M5C 2G5 
           [short_name] => M5C 2G5 
           [type] => postal_code 
          ) 

        ) 

       [geometry] => SimpleXMLElement Object 
        (
         [location] => SimpleXMLElement Object 
          (
           [lat] => 43.6547363 
           [lng] => -79.3763746 
          ) 

         [location_type] => RANGE_INTERPOLATED 
         [viewport] => SimpleXMLElement Object 
          (
           [southwest] => SimpleXMLElement Object 
            (
             [lat] => 43.6533849 
             [lng] => -79.3777345 
            ) 

           [northeast] => SimpleXMLElement Object 
            (
             [lat] => 43.6560829 
             [lng] => -79.3750365 
            ) 

          ) 

         [bounds] => SimpleXMLElement Object 
          (
           [southwest] => SimpleXMLElement Object 
            (
             [lat] => 43.6547315 
             [lng] => -79.3763964 
            ) 

           [northeast] => SimpleXMLElement Object 
            (
             [lat] => 43.6547363 
             [lng] => -79.3763746 
            ) 

          ) 

        ) 

      ) 

答えて

0

私はこれを試していないが、私はそれが動作するはずだと思います。

$address = $array->result[0]->address_component; 
$postal_code = ''; 
foreach($address as $adr){ 
    if($adr->type == 'postal_code'){ 
     $postal_code = $adr->long_name; 
    } 
} 
+0

ありがとうございました!それは完璧に働いた。これを長い間把握しようとしていた。今私はいくつかの睡眠を得ることができます。 – DanielAttard