我想将此字符串转换为正确的关联数组。
请点击链接查看字符串数据。
https://maps.googleapis.com/maps/api/geocode/json?latlng=16.539311299999998,80.5820222
我希望将其转换为适当的Associaitve数组。
我看起来像这样。echo $ array ['long_name']应返回类似'Attlee Road'的值
请帮我这个,我试试2天,尝试所有爆炸,内爆,foreachloops,我很困惑,请有人帮我这个。
下面的代码将回显字符串数据
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . ";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>
答案 0 :(得分:1)
在输出中使用json_decode()
答案 1 :(得分:1)
您必须使用json_decode()
功能。
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . "&key=AIzaSyDCgyjLTrpadabEAyDNXf2GPpgChvpMw_4";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
$returned_contents_array = json_decode($returned_contents, true);
$results = $returned_contents_array['results'];
foreach($results as $result) {
echo $result['address_components'][0]['long_name'];
}
?>
答案 2 :(得分:0)
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . "&key=AIzaSyDCgyjLTrpadabEAyDNXf2GPpgChvpMw_4";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
$returned_contents = json_decode($returned_contents);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>