Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a JSON string that I've converted into a associative PHP array using "json_decode". For some reason I can't seem to figure out how to find the index path to the value I'm looking for inside the array.

Here's the JSON string:

    {
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "175 Hemenway St, Boston, MA 02115, USA",
    "address_components": [ {
      "long_name": "175",
      "short_name": "175",
      "types": [ "street_number" ]
    }, {
      "long_name": "Hemenway St",
      "short_name": "Hemenway St",
      "types": [ "route" ]
    }, {
      "long_name": "Boston",
      "short_name": "Boston",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Boston",
      "short_name": "Boston",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Suffolk",
      "short_name": "Suffolk",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Massachusetts",
      "short_name": "MA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "02115",
      "short_name": "02115",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 42.3411740,
        "lng": -71.0912860
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 42.3380264,
          "lng": -71.0944336
        },
        "northeast": {
          "lat": 42.3443216,
          "lng": -71.0881384
        }
      }
    }
  } ]
}

and here is the var_dump output of the associative array in PHP

array(2) { ["status"]=> string(2) "OK" ["results"]=> array(1) { [0]=> array(4) { ["types"]=> array(1) { [0]=> string(14) "street_address" } ["formatted_address"]=> string(38) "175 Hemenway St, Boston, MA 02115, USA" ["address_components"]=> array(8) { [0]=> array(3) { ["long_name"]=> string(3) "175" ["short_name"]=> string(3) "175" ["types"]=> array(1) { [0]=> string(13) "street_number" } } [1]=> array(3) { ["long_name"]=> string(11) "Hemenway St" ["short_name"]=> string(11) "Hemenway St" ["types"]=> array(1) { [0]=> string(5) "route" } } [2]=> array(3) { ["long_name"]=> string(6) "Boston" ["short_name"]=> string(6) "Boston" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [3]=> array(3) { ["long_name"]=> string(6) "Boston" ["short_name"]=> string(6) "Boston" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_3" [1]=> string(9) "political" } } [4]=> array(3) { ["long_name"]=> string(7) "Suffolk" ["short_name"]=> string(7) "Suffolk" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_2" [1]=> string(9) "political" } } [5]=> array(3) { ["long_name"]=> string(13) "Massachusetts" ["short_name"]=> string(2) "MA" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [6]=> array(3) { ["long_name"]=> string(13) "United States" ["short_name"]=> string(2) "US" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } [7]=> array(3) { ["long_name"]=> string(5) "02115" ["short_name"]=> string(5) "02115" ["types"]=> array(1) { [0]=> string(11) "postal_code" } } } ["geometry"]=> array(3) { ["location"]=> array(2) { ["lat"]=> float(42.341174) ["lng"]=> float(-71.091286) } ["location_type"]=> string(7) "ROOFTOP" ["viewport"]=> array(2) { ["southwest"]=> array(2) { ["lat"]=> float(42.3380264) ["lng"]=> float(-71.0944336) } ["northeast"]=> array(2) { ["lat"]=> float(42.3443216) ["lng"]=> float(-71.0881384) } } } } } }

Ideally I would like the array under the index "location" (the values under the lat & lng keys)

I can't seem to get much further than array_name["results"]

Is there maybe an alternative to var_dump that will line-separate and indent multi-level arrays so that I can understand them? I'd rather work with a direct path than recursively search through the entire array to find what I'm looking for. Thanks!

share|improve this question
up vote 3 down vote accepted

Have you tried to access "location" this way?:

$location = $array_name['results'][0]['geometry']['location'];
$lat = $location['lat'];
$lng = $location['lng'];
share|improve this answer
    
That's it, thanks @scoffey. I have a lot of difficulty matching up starting/ending brackets and determining when one array ends and the other begins. Is there a good technique or are you just very good at looking at strings of data and making sense of them? haha – Casey Flynn Jan 17 '11 at 20:07
    
Good editors like vim highlight matching opening/closing brackets. By the way, print_r is an alternative to var_dump. See: stackoverflow.com/questions/1168175/… – scoffey Jan 17 '11 at 20:10

Is there maybe an alternative to var_dump that will line-separate and indent multi-level arrays so that I can understand them?

Print_r, var dump and var export all output as separate multi-line text.

http://ca2.php.net/print_r
http://ca2.php.net/manual/en/function.var-dump.php
http://ca2.php.net/manual/en/function.var-export.php

You just aren't seeing it because you're viewing the rendered results of an HTML document, rather than the raw-text. View source, or replace new lines with <br/> HTML elements, or enclose the output in <pre> tags.

Remember, browsers tend to ignore whitespace such as new lines and indents when rendering.

share|improve this answer
    
Ah of course; that makes sense. Thanks @user257493. :) – Casey Flynn Jan 17 '11 at 20:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.