【问题标题】:How to Properly Convert String to Associative Array in PHP如何在 PHP 中正确地将字符串转换为关联数组
【发布时间】:2017-06-10 03:45:26
【问题描述】:

我想将此字符串转换为适当的关联数组。

请点击链接查看字符串数据。

https://maps.googleapis.com/maps/api/geocode/json?latlng=16.539311299999998,80.5820222

我希望将其转换为适当的关联数组。

我看起来像这样。

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>";

?>

【问题讨论】:

    标签: php arrays json string explode


    【解决方案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】:

      在输出中使用 json_decode()

      【讨论】:

        【解决方案3】:

        试试看

        <?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>";
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-04
          • 2018-01-27
          • 1970-01-01
          • 2012-06-10
          • 2019-06-26
          相关资源
          最近更新 更多