2017-07-21 12 views
-2

私はどこでも答えを探していますが、見つからなかった。私はLat、LongからAddressを表示するためにgoogle map geolocation APIを使用しています。私は配列の"formatted_address"のような選択的なデータだけを表示する必要があります。これは私がPHPで応答を表示しようとしているコードです:GOOGLE MAPS GEOLOCATION APIのPHPからの応答を取得すると、出力はJSONになります

$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true); 
$data = json_decode($file_contents, true); 

foreach($data as $output => $var) { 
    echo $var; 
} 

はこれが応答である:[https://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875]

これは私が手出力されます:

Notice: Array to string conversion in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
ArrayOK

私がしようとすると、

$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true); 
$data = json_decode($file_contents, true); 

foreach($data as $output => $var) { 
    echo $var['formatted_address']; 
} 

私はこのエラーを受け取ります:

Notice: Undefined index: formatted_address in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176

Warning: Illegal string offset 'formatted_address' in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
O

私はここで間違っていますか?

ありがとうございます!

答えて

1

の代わりに、(あなたの最初のコードセクション内)エコーがしますprint_r($のVAR)

$file_contents = 
file_get_contents("http://maps.googleapis.com/maps/api/geocode/json? 
latlng=31.334097,74.235875&sensor=false", true); 
$data = json_decode($file_contents, true); 

foreach($data as $output => $var) { 
    print_r($var); 
} 

を使用これはあなたがあなたが正確にアクセスすることができ

を欠けている値へのパスを確認することができますこのコードでお望みの価値

$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json? 
latlng=31.334097,74.235875&sensor=false", true); 
$data = json_decode($file_contents, true); 

$formatted_address = $data['results']['0']['formatted_address']; 
echo $formatted_address; 
+0

素晴らしい!トリックをしました。本当にありがとう! –

+0

全く問題ありません:) – Solor

関連する問題