如何在PHP中正确地将字符串转换为关联数组
我想将此字符串转换为正确的关联数组 请点击链接查看字符串数据 我想把它转换成一个适当的关联数组 我看起来像这样 echo$array['long_name']应返回类似于'Attlee Road'的值 请帮助我,我试了两天,试了所有的爆炸,内爆,前驱循环,我很困惑,请有人帮我 下面的代码将回显字符串数据如何在PHP中正确地将字符串转换为关联数组,php,arrays,json,string,explode,Php,Arrays,Json,String,Explode,我想将此字符串转换为正确的关联数组 请点击链接查看字符串数据 我想把它转换成一个适当的关联数组 我看起来像这样 echo$array['long_name']应返回类似于'Attlee Road'的值 请帮助我,我试了两天,试了所有的爆炸,内爆,前驱循环,我很困惑,请有人帮我 下面的代码将回显字符串数据 <?php $lat = 16.539311299999998; $lng = 80.5820222; $url = "https://maps.googleapis.com/map
<?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>";
?>
在输出上使用json_decode()在输出上使用json_decode()您必须使用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 = json_decode($returned_contents);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>
您必须使用json\u 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 = json_decode($returned_contents);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>
试试看
试试看