user1105787
user1105787

Reputation:

Json Decoding Trouble

here's my decoded json output,

object(stdClass)#1 (3) { ["name"]=> string(34) "xxxxxxxxxxxxxxx" ["Status"]=> object(stdClass)#2 (2) { ["code"]=> int(200) ["request"]=> string(7) "geocode" } ["Placemark"]=> array(1) { [0]=> object(stdClass)#3 (5) { ["id"]=> string(2) "p1" ["address"]=> string(32) "xxxxxxxxxxxxxxx, USA" ["AddressDetails"]=> object(stdClass)#4 (2) { ["Accuracy"]=> int(8) ["Country"]=> object(stdClass)#5 (3) { ["AdministrativeArea"]=> object(stdClass)#6 (2) { ["AdministrativeAreaName"]=> string(2) "UT" ["Locality"]=> object(stdClass)#7 (3) { ["LocalityName"]=> string(4) "xxxxxxxxxxxxxxx" ["PostalCode"]=> object(stdClass)#8 (1) { ["PostalCodeNumber"]=> string(5) "xxxxxxxxxxxxxxx" } ["Thoroughfare"]=> object(stdClass)#9 (1) { ["ThoroughfareName"]=> string(7) "xxxxxxxxxxxxxxx" } } } ["CountryName"]=> string(3) "USA" ["CountryNameCode"]=> string(2) "US" } } ["ExtendedData"]=> object(stdClass)#10 (1) { ["LatLonBox"]=> object(stdClass)#11 (4) { ["north"]=> float(xxxxxxxxxxxxxxx) ["south"]=> float(xxxxxxxxxxxxxxx) ["east"]=> float(-111.7018448) ["west"]=> float(xxxxxxxxxxxxxxx) } } ["Point"]=> object(stdClass)#12 (1) { ["coordinates"]=> array(3) { [0]=> float(xxxxxxxxxxxxxxx) [1]=> float(xxxxxxxxxxxxxxx) [2]=> int(0) } } } } }

I can retrieve the zipcode (84058) by using

echo $geo->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber; 

but how in the world would I retrieve these objects?:

  1. ["north"]=> float(40.2824822)
  2. ["east"]=> float(-111.7018448)
  3. [0]=>float(-111.7032032)
  4. [1]=> float(40.2811332)

Thanks, i'm new to this.

Upvotes: 0

Views: 190

Answers (2)

Paul
Paul

Reputation: 141829

1.    $geo->Placemark[0]->ExtendedData->LatLonBox->north;
2.    $geo->Placemark[0]->ExtendedData->LatLonBox->east;

3.    $geo->Placemark[0]->Point->coordinates[0];
4.    $geo->Placemark[0]->Point->coordinates[1];

It's a lot easy to see if you try running it through a pretty printer:

object(stdClass)#1 (3) {
    ["name"]=> string(34) "xxxxxxxxxxxxxxx" ["Status"]=> object(stdClass)#2 (2) {
        ["code"]=> int(200) ["request"]=> string(7) "geocode"
    }
    ["Placemark"]=> array(1) {
        [0]=> object(stdClass)#3 (5) {
            ["id"]=> string(2) "p1" ["address"]=> string(32) "xxxxxxxxxxxxxxx, USA" ["AddressDetails"]=> object(stdClass)#4 (2) {
                ["Accuracy"]=> int(8) ["Country"]=> object(stdClass)#5 (3) {
                    ["AdministrativeArea"]=> object(stdClass)#6 (2) {
                        ["AdministrativeAreaName"]=> string(2) "UT" ["Locality"]=> object(stdClass)#7 (3) {
                            ["LocalityName"]=> string(4) "Orem" ["PostalCode"]=> object(stdClass)#8 (1) {
                                ["PostalCodeNumber"]=> string(5) "xxx"
                            }
                            ["Thoroughfare"]=> object(stdClass)#9 (1) {
                                ["ThoroughfareName"]=> string(7) "S 325 W"
                            }

                        }

                    }
                    ["CountryName"]=> string(3) "USA" ["CountryNameCode"]=> string(2) "US"
                }

            }
            ["ExtendedData"]=> object(stdClass)#10 (1) {
                ["LatLonBox"]=> object(stdClass)#11 (4) {
                    ["north"]=> float(xxx) ["south"]=> float(40.2797843) ["east"]=> float(-111.7018448) ["west"]=> float(xxxx)
                }

            }
            ["Point"]=> object(stdClass)#12 (1) {
                ["coordinates"]=> array(3) {
                    [0]=> float(xxxxx) [1]=> float(xxxx) [2]=> int(0)
                }

            }

        }

    }

}

Upvotes: 0

Francis Lewis
Francis Lewis

Reputation: 8980

You would retrieve it the exact same way you're retrieving the zipcode. Break it down so you can understand it:

// access the entire json object
$geo

// access the placemark object (which contains an array of data)
$geo->Placemark

// access the placemark object at index 0
$geo->Placemark[0]

// access the Extended Data object of the Placemark object
$geo->Placemark[0]->ExtendedData

And so on. So now you should be able to figure out how to reach all the way to the north, south, east and west elements.

echo $geo->Placemark[0]->ExtendedData->LatLonBox->north;
echo $geo->Placemark[0]->ExtendedData->LatLonBox->south;

etc...

Upvotes: 1

Related Questions