2013-11-23 3 views
0

에서 JSON 파일에 액세스 :은 내가 JSON의이 같은 파일이 PHP

"location" : { 
     "lat" : 48.0418838, 
     "lng" : 14.4078882 
    }, 
: 나는 정의되지 않은 인덱스 위치를 얻을 액세스하려고

{"results" : [ 
    { 
    "address_components" : [ 
     { 
      "long_name" : "4400", 
      "short_name" : "4400", 
      "types" : [ "postal_code" ] 
     }, 
     { 
      "long_name" : "Upper Austria", 
      "short_name" : "OÖ", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "Austria", 
      "short_name" : "AT", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "4400, Austria", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 48.06669549999999, 
       "lng" : 14.4687485 
      }, 
      "southwest" : { 
       "lat" : 47.9987654, 
       "lng" : 14.3408425 
      } 
     }, 
     "location" : { 
      "lat" : 48.0418838, 
      "lng" : 14.4078882 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 48.06669549999999, 
       "lng" : 14.4687485 
      }, 
      "southwest" : { 
       "lat" : 47.9987654, 
       "lng" : 14.3408425 
      } 
     } 
    }, 
    "types" : [ "postal_code" ] 
    } 
], 
"status" : "OK" 
} 

과 매번, 나는이 부분에 액세스하려면

with this :

$url1="http://maps.googleapis.com/maps/api/geocode/json?address=AT-".$a."&sensor=false"; 
$unparsed_json = file_get_contents($url1); 
$data =(array) json_decode($unparsed_json); 
echo $unparsed_json; 
$latlng= $data['results']['location']['lat']; 
echo $latlng; 

하지만 결과는 null입니다. 어떤 도움이라도 대단히 감사 할 것입니다.

답변

2

json 코드에서 배열을 사용하고 있으며 이에 따라 항목을 참조해야하며 위치가 기하 구조 엔티티에 있습니다. 함께 시험해보기 $latlng= $data['results'][0]['geometry']['location']['lat'];

JS의 개념을 보여주기 위해 여기에 demo도 있습니다. 위의 코드를 PHP 코드에서 사용하십시오.

+0

위대한, 나를 구해줬습니다. – enigmaticus

+0

@juliesmith는 그 당시의 대답을 표시했습니다. 그렇지 않으면 미래에 귀하의 질문에 대한 답변의 동기가 줄어들 것입니다. – Avery

+0

나는 그것을 알고있다. 그러나 내가 그것을 받아들이 기 전에 당신이 기다릴 필요가 있기 때문에 나는 그것을 즉시 할 수 없었다. :) – enigmaticus

관련 문제