如何从数组中选择值

问题描述 投票:0回答:1

我正在从Here Maps获取PHP数组:

array(1) {
["Response"] => array(2) {
    ["MetaInfo"] => array(1) {
        ["Timestamp"] => string(28)
        "2020-04-26T17:28:14.089+0000"
    } ["View"] => array(1) {
        [0] => array(3) {
            ["_type"] => string(21)
            "SearchResultsViewType" ["ViewId"] => int(0)["Result"] => array(1) {
                [0] => array(5) {
                    ["Relevance"] => float(1)["MatchLevel"] => string(11)
                    "houseNumber" ["MatchQuality"] => array(4) {
                        ["Country"] => float(1)["City"] => float(1)["Street"] => array(1) {
                            [0] => float(0.9)
                        } ["HouseNumber"] => float(1)
                    } ["MatchType"] => string(12)
                    "pointAddress" ["Location"] => array(6) {
                        ["LocationId"] => string(28)
                        "NT_uIrE4zNUPdurm.zAQNkxHA_0A" ["LocationType"] => string(7)
                        "address" ["DisplayPosition"] => array(2) {
                            ["Latitude"] => float(52.14242)["Longitude"] => float(20.71666)
                        } ["NavigationPosition"] => array(1) {
                            [0] => array(2) {
                                ["Latitude"] => float(52.14251)["Longitude"] => float(20.71668)
                            }
                        } ["MapView"] => array(2) {
                            ["TopLeft"] => array(2) {
                                ["Latitude"] => float(52.1435442)["Longitude"] => float(20.7148282)
                            } ["BottomRight"] => array(2) {
                                ["Latitude"] => float(52.1412958)["Longitude"] => float(20.7184918)
                            }
                        } ["Address"] => array(10) {
                            ["Label"] => string(38)
                            "ulica Rynek 4, 05-840 Brwinów, Polska" ["Country"] => string(3)
                            "POL" ["State"] => string(16)
                            "Woj. Mazowieckie" ["County"] => string(18)
                            "Powiat Pruszkowski" ["City"] => string(8)
                            "Brwinów" ["District"] => string(8)
                            "Brwinów" ["Street"] => string(11)
                            "ulica Rynek" ["HouseNumber"] => string(1)
                            "4" ["PostalCode"] => string(6)
                            "05-840" ["AdditionalData"] => array(3) {
                                [0] => array(2) {
                                    ["value"] => string(6)
                                    "Polska" ["key"] => string(11)
                                    "CountryName"
                                } [1] => array(2) {
                                    ["value"] => string(16)
                                    "Woj. Mazowieckie" ["key"] => string(9)
                                    "StateName"
                                } [2] => array(2) {
                                    ["value"] => string(18)
                                    "Powiat Pruszkowski" ["key"] => string(10)
                                    "CountyName"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

}

要在上面粘贴数组,我正在使用:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$string = curl_setopt($ch, CURLOPT_URL, "https://geocoder.ls.hereapi.com/6.2/geocode.json?&street=Rynek%204&city=Brwin%C3%B3w%20&country=Poland&gen=9&apiKey=MY_API_KEY");

$result=curl_exec($ch);


curl_close($ch);

$decoded=var_dump(json_decode($result, true));

我想从DisplayPosition获取纬度经度。我该怎么做?

我尝试过:var_dump($ decoded [1] [“ Response”] [0] [“ View”] [0] [“ Result”] [0] [“ Location”] [“ DisplayPosition”] [“ Latitude”]);

但不会帮上忙。我想我尝试了一切...

php arrays
1个回答
1
投票

您没有得到作为数组的答复。您得到的是JSON。据我了解,您将响应放入一个名为$string的变量中(基于对@Sacha的注释)。

您要将其视为数组,请尝试做:

//true makes the json string into an associative arrays which
//is often needed for json responses 
$array = json_decode($string, true); 

完成此操作后,请尝试以下操作:

echo '<pre>';
print_r($array);
echo '</pre>';

我的猜测是可能是这样:

$latitude = $array['Response']['View'][0]['Location']['DisplayPosition']['Latitude']
$longitudfe = $array['Response']['View'][0]['Location']['DisplayPosition']['Longitude']

但是正如@Sacha所述,您必须检查值级别以确保您在正确的轨道上:

var_dump($array['Response']);
var_dump($array['Response']['View'];
var_dump($array['Response']['View'][0];

等...

© www.soinside.com 2019 - 2024. All rights reserved.