У меня есть следующий массив 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, как мне получить значения geometery->location->lat & lng из моего массива JSON выше?
Например (псевдокод):
<?php $lat = $json['geometry']['location']['lat']; // 41.9858860 $lng = $json['geometry']['location']['lng']; // -87.7907460 ?>
Вы используете json_decode а затем $var->results[0]->geometry->location->lat; ,
json_decode дает следующую структуру:
object ( stdClass ) [ 1 ]
public 'status' => string 'OK' (length = 2)
публичные «результаты» =>
массив
0 =>
object ( stdClass ) [ 2 ]
public 'types' =>
массив
0 => строка 'street_address' (length = 14)
public 'formatted_address' => string '5721 N Northcott Ave, Чикаго, IL 60631, США' (длина = 44)
public 'address_components' =>
массив
0 =>
object ( stdClass ) [ 3 ]
public 'long_name' => string '5721' (length = 4)
public 'short_name' => строка '5721' (длина = 4)
public 'types' =>
массив
0 => строка 'street_number' (length = 13)
1 =>
объект ( stdClass ) [ 4 ]
public 'long_name' => string 'Illinois' (length = 8)
public 'short_name' => string 'IL' (длина = 2)
public 'types' =>
массив
0 => строка 'Administrative_area_level_1' (length = 27)
1 => строка «политическая» (длина = 9)
2 =>
object ( stdClass ) [ 5 ]
public 'long_name' => string '60631' (length = 5)
public 'short_name' => string '60631' (length = 5)
public 'types' =>
массив
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' => строка 'ROOFTOP' (длина = 7)
public 'viewport' =>
object ( stdClass ) [ 8 ]
общественный «юго-запад» =>
object ( stdClass ) [ 9 ]
public 'lat' => float 41.9827384
public 'lng' => float -87.7938936
общественный 'северо-восток' =>
object ( stdClass ) [ 10 ]
public 'lat' => float 41.9890336
public 'lng' => float -87.7875984
Вы используете json_decode .
Например:
$json_obj = json_decode($json); echo $json_obj->results[0]->geometry->location->lat;
Мы обращаемся к первому элементу массива результатов, затем переходим через геометрию, местоположение и свойства lat. Вы также можете использовать ассоциативные массивы, но по умолчанию это объект.
Приветствую,
Я рекомендую прочитать документацию json_decode
<?php $obj = json_decode($json); $lat = $obj->results[0]->geometry->location->lat; ?>