Я получаю данные JSON от https://nominatim.openstreetmap.org/[email protected]&format=json&q=Berlin";, который возвращает
[
{
"place_id": 574401,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 240109189,
"boundingbox": [
"52.3570365",
"52.6770365",
"13.2288599",
"13.5488599"
],
"lat": "52.5170365",
"lon": "13.3888599",
"display_name": "Berlin, 10117, Deutschland",
"class": "place",
"type": "city",
"importance": 0.8975390282491362,
"icon": "https://nominatim.openstreetmap.org/ui/mapicons//poi_place_city.p.20.png"
},
{
"place_id": 256375666,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "relation",
"osm_id": 62422,
"boundingbox": [
"52.3382448",
"52.6755087",
"13.088345",
"13.7611609"
],
"lat": "52.5015217",
"lon": "13.4025498",
"display_name": "Berlin, Deutschland",
"class": "boundary",
"type": "administrative",
"importance": 0.8975390282491362,
"icon": "https://nominatim.openstreetmap.org/ui/mapicons//poi_boundary_administrative.p.20.png"
}
]
Теперь я пытаюсь получить доступ к ключу lat и lon первого ключа. Для этого я бы преобразовал его, используя $new_resp=json_decode($response, true), который возвращает String «Array». Как я могу получить первые значения долготы и широты?
Спасибо за разъяснения!






Try this once.
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'https://nominatim.openstreetmap.org/[email protected]&format=json&q=Berlin');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$jsonData = json_decode(curl_exec($curlSession),true);
print_r($jsonData[0]['lat']); //This prints your first lat
print_r($jsonData[0]['lon']); //This prints your first lon
//if you want lat & lon for every record then use foreach loop.
curl_close($curlSession);
Работает отлично, однако в строке 10 допущена опечатка: должно быть ['lon'].
Отредактированный ответ на опечатку, спасибо !!
echo. Используйтеvar_dumpилиprint_rдля вывода отладки массивов или других сложных объектов.