2010-07-08 78 views
2

我有以下的JSON數組:PHP:我如何從JSON數組中獲取屬性?

<?php  
$json = { 
    "status": "OK", 
    "results": [ { 
    "types": [ "street_address" ], 
    "formatted_address": "5721 N Northcott Ave, Chicago, IL 60631, USA", 
    "address_components": [ { 
     "long_name": "5721", 
     "short_name": "5721", 
     "types": [ "street_number" ] 
    }, { 
     "long_name": "Illinois", 
     "short_name": "IL", 
     "types": [ "administrative_area_level_1", "political" ] 
    }, { 
     "long_name": "60631", 
     "short_name": "60631", 
     "types": [ "postal_code" ] 
    } ], 
    "geometry": { 
     "location": { 
     "lat": 41.9858860, 
     "lng": -87.7907460 
     }, 
     "location_type": "ROOFTOP", 
     "viewport": { 
     "southwest": { 
      "lat": 41.9827384, 
      "lng": -87.7938936 
     }, 
     "northeast": { 
      "lat": 41.9890336, 
      "lng": -87.7875984 
     } 
     } 
    } 
    } ] 
}; 
?> 

使用PHP,我如何才能從上面我的JSON數組的geometery->location->lat & lng值?

例如(僞代碼):

<?php 
$lat = $json['geometry']['location']['lat']; // 41.9858860 
$lng = $json['geometry']['location']['lng']; // -87.7907460 
?> 

回答

5

您使用json_decode,然後使用$var->results[0]->geometry->location->lat;

json_decode產生的結構如下:

 
object(stdClass)[1] 
    public 'status' => string 'OK' (length=2) 
    public 'results' => 
    array 
     0 => 
     object(stdClass)[2] 
      public 'types' => 
      array 
       0 => string 'street_address' (length=14) 
      public 'formatted_address' => string '5721 N Northcott Ave, Chicago, IL 60631, USA' (length=44) 
      public 'address_components' => 
      array 
       0 => 
       object(stdClass)[3] 
        public 'long_name' => string '5721' (length=4) 
        public 'short_name' => string '5721' (length=4) 
        public 'types' => 
        array 
         0 => string 'street_number' (length=13) 
       1 => 
       object(stdClass)[4] 
        public 'long_name' => string 'Illinois' (length=8) 
        public 'short_name' => string 'IL' (length=2) 
        public 'types' => 
        array 
         0 => string 'administrative_area_level_1' (length=27) 
         1 => string 'political' (length=9) 
       2 => 
       object(stdClass)[5] 
        public 'long_name' => string '60631' (length=5) 
        public 'short_name' => string '60631' (length=5) 
        public 'types' => 
        array 
         0 => string 'postal_code' (length=11) 
      public 'geometry' => 
      object(stdClass)[6] 
       public 'location' => 
       object(stdClass)[7] 
        public 'lat' => float 41.985886 
        public 'lng' => float -87.790746 
       public 'location_type' => string 'ROOFTOP' (length=7) 
       public 'viewport' => 
       object(stdClass)[8] 
        public 'southwest' => 
        object(stdClass)[9] 
         public 'lat' => float 41.9827384 
         public 'lng' => float -87.7938936 
        public 'northeast' => 
        object(stdClass)[10] 
         public 'lat' => float 41.9890336 
         public 'lng' => float -87.7875984 
3

您使用json_decode

例如爲:

$json_obj = json_decode($json); 
echo $json_obj->results[0]->geometry->location->lat; 

我們訪問結果陣列的第一個元素,然後通過幾何形狀,位置,和LAT屬性導航。你也可以使用關聯數組,但默認是一個對象。

+0

如果我使用json_decode,然後呢?我如何參考LAT和LNG值? – TeddyR 2010-07-08 00:17:49

+0

我已經閱讀過這個鏈接中的例子,但仍然無法弄清楚。這就是爲什麼我現在發佈這個問題到現在 – TeddyR 2010-07-08 00:21:47

2

問候,

我建議你閱讀json_decode文檔

<?php 
    $obj = json_decode($json); 
    $lat = $obj->results[0]->geometry->location->lat; 
    ?> 
+0

我實際上已經閱讀了該鏈接中的例子,仍然無法弄清楚。這就是爲什麼我現在要發佈這個問題。 – TeddyR 2010-07-08 00:21:15

+0

Righteo。那麼我希望你已經整理好了。 – kobrien 2010-07-08 00:37:08