www.webdeveloper.com
Results 1 to 2 of 2

Thread: json_decode Usage with multi dimension JSON code

  1. #1
    Join Date
    Nov 2007
    Posts
    15

    json_decode Usage with multi dimension JSON code

    I am having proble to extract a vlaue from this code

    { "name": "1600 Amphitheatre Parkway, Mountain View, CA", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "AddressDetails": {"Country": {"CountryNameCode": "US","CountryName": "USA","AdministrativeArea": {"AdministrativeAreaName": "CA","Locality": {"LocalityName": "Mountain View","Thoroughfare":{"ThoroughfareName": "1600 Amphitheatre Pkwy"},"PostalCode": {"PostalCodeNumber": "94043"}}}},"Accuracy": 8}, "ExtendedData": { "LatLonBox": { "north": 37.4251196, "south": 37.4188244, "east": -122.0809954, "west": -122.0872906 } }, "Point": { "coordinates": [ -122.0841430, 37.4219720, 0 ] } } ] }

    I want to extract this value
    -122.0841430

    Using PHP json_decode (object or array), json_decode works fine for me with simple files but it doesnt work here for me here, any ideas?

  2. #2
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    You could try something like this. (Fast and dirty) or look into recursion instead of the nested foreach loop.

    PHP Code:
    <?php
      $json 
    '{ 
        "name": "1600 Amphitheatre Parkway, Mountain View, CA", "Status": 
        { 
            "code": 200, "request": "geocode" 
        }, 
        "Placemark": [ 
           { 
            "id": "p1", "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "AddressDetails": 
            {
                "Country": 
                {
                    "CountryNameCode": "US","CountryName": "USA","AdministrativeArea": 
                    {
                        "AdministrativeAreaName": "CA","Locality": 
                        {
                            "LocalityName": "Mountain View","Thoroughfare":
                            {
                                "ThoroughfareName": "1600 Amphitheatre Pkwy"
                            }
                            ,"PostalCode": 
                            {
                                "PostalCodeNumber": "94043"
                            }
                        }
                    }
                }
                ,"Accuracy": 8
            }
            , "ExtendedData": 
            { 
                "LatLonBox": 
                { 
                    "north": 37.4251196, "south": 37.4188244, "east": -122.0809954, "west": -122.0872906 
                } 
            }
            , "Point": 
            { 
                "coordinates": [ -122.0841430, 37.4219720, 0 ] 
            } 
        } 
        ] 
    }'
    ;


    $arr json_decode($jsontrue);

    foreach(
    $arr as $key1 => $value1) {
        echo 
    "<b>Key1: </b>" $key1 " <b>value1: </b>" $value1 "<br />";
        
        if(
    is_array($value1)) {
            foreach(
    $value1 as $key2 => $value2) {
                echo 
    "<b>Key2: </b>" $key2 " <b>value2: </b>" $value2 "<br />";
                
                if(
    is_array($value2)) {
                    foreach(
    $value2 as $key3 => $value3) {
                        echo 
    "<b>Key3: </b>" $key3 " <b>value3: </b>" $value3 "<br />";
                        
                        if(
    is_array($value3)) {
                            foreach(
    $value3 as $key4 => $value4) {
                                echo 
    "<b>Key4: </b>" $key4 " <b>value4: </b>" $value4 "<br />";
                                
                                if(
    is_array($value4)) {
                                    foreach(
    $value4 as $key5 => $value5) {
                                        echo 
    "<b>Key5: </b>" $key5 " <b>value5: </b>" $value5 "<br />";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles