0

I purchased an API that does reverse lookup given the lat/lon. I want to extract from the results the state name and state abbreviation. It had been working but something changed and now I'm getting PHP error messages.

$json = @file_get_contents($url);
$result = array_values(json_decode($json, true));
print_r($result);

The result of this call looks like the following. How do I set a variables to equal the State Name and the state abbreviation?

Array
(
    [0] => FeatureCollection
    [1] => Array
        (
            [0] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [datasource] => Array
                                (
                                    [sourcename] => openstreetmap
                                    [attribution] => © OpenStreetMap contributors
                                    [license] => Open Database License
                                    [url] => https://www.openstreetmap.org/copyright
                                )

                            [country] => United States
                            [country_code] => us
                            [state] => North Carolina
                            [county] => Cumberland County
                            [city] => Hope Mills
                            [hamlet] => Timberlake
                            [postcode] => 28348
                            [street] => Labonte Drive
                            [housenumber] => 5941
                            [lon] => -78.976173283181
                            [lat] => 34.974986403843
                            [state_code] => NC
                            [distance] => 8.514796509686
                            [result_type] => building
                            [formatted] => 5941 Labonte Drive, Hope Mills, NC 28348, United States of America
                            [address_line1] => 5941 Labonte Drive
                            [address_line2] => Hope Mills, NC 28348, United States of America
                            [timezone] => Array
                                (
                                    [name] => America/New_York
                                    [offset_STD] => -05:00
                                    [offset_STD_seconds] => -18000
                                    [offset_DST] => -04:00
                                    [offset_DST_seconds] => -14400
                                    [abbreviation_STD] => EST
                                    [abbreviation_DST] => EDT
                                )

                            [rank] => Array
                                (
                                    [importance] => -1.15
                                    [popularity] => 1.8893922812046
                                )

                            [place_id] => 51809f819f79be53c0596246bf5acc7c4140f00102f901f3abfb0000000000c00203
                        )

                    [geometry] => Array
                        (
                            [type] => Point
                            [coordinates] => Array
                                (
                                    [0] => -78.976173283181
                                    [1] => 34.974986403843
                                )

                        )

                    [bbox] => Array
                        (
                            [0] => -78.976223283181
                            [1] => 34.974936403843
                            [2] => -78.976123283181
                            [3] => 34.975036403843
                        )

                )

        )

)
user3384413
  • 91
  • 1
  • 8
  • Looks like `$result[1][0]['properties']['state']` and `$result[1][0]['properties']['state_code']` https://3v4l.org/mRre6 – Nick May 26 '23 at 03:07
  • Use `var_export` for sample data in your questions, then other users can simply cut and paste the data into PHP to test answers. – Nick May 26 '23 at 03:08
  • Nick, Thanks! I just didn't figure out the [1],[0] part. – user3384413 May 28 '23 at 18:57

0 Answers0