0

I'm getting PHP array from Here Maps:

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

}

To got array pasted above I'm using:

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

I would like to get Latitude and Longitude from DisplayPosition. How I can do this?

I tried: var_dump ($decoded[1]["Response"][0]["View"][0]["Result"][0]["Location"]["DisplayPosition"]["Latitude"]);

but it won't helpt. I think I tried everything...

7
  • It's very hard to read your array. Better show var_export($decoded). But your array starts with $decoded['Response']['View'][0]['Result'][0]... Commented Apr 26, 2020 at 19:44
  • @Justinas var_export($decoded); is giving me NULL Commented Apr 26, 2020 at 19:47
  • What is your array called? Commented Apr 26, 2020 at 19:57
  • @mrfox And what if you do echo var_export($decoded, true);? Does your array is named $decoded? Commented Apr 26, 2020 at 20:02
  • When I try echo var_export($decoded, true); I'm getting still NULL Commented Apr 26, 2020 at 20:04

1 Answer 1

1

You don't get your response as an array. You get as an JSON. As I understand it you put the response in a variable you call $string (based on comments to @Sacha).

You want to treat this as an array, try doing:

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

When you have done this, try this:

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

My guess is that it might be something like this:

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

But as @Sacha mentioned, you have to check value level for level to be sure that you're on the right track:

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

etc...

Sign up to request clarification or add additional context in comments.

6 Comments

First of all I know that array is hard to read :/ I'm getting it from Here Maps service. $latitude is empty. Even when I shortened $decoded['Response']['View'][0]['Location']['DisplayPosition']['Latitude'] from you into $decoded['Response']
Aha. This is not an array. It's a JSON-formatted response.
Arrrrr. So it looks I made nice mess here
@mrfox .- please look at my updated answer and I hope it will give you the answer you needed :-)
You saved me. Thank you so much. You resolved problem I made. Once again, thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.