Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:58:21+00:00 2026-06-09T04:58:21+00:00

I want to add the locality name from a google maps html call(below) to

  • 0

I want to add the locality name from a google maps html call(below) to a specific php variable so that I can 1) store it in a mysql database and 2) display it as part of an echo statement (echo $locality).

http://maps.googleapis.com/maps/api/geocode/json?latlng=34.11375320612877,-118.35220470764159&sensor=false

I have decoded the json data via: json_decode($data,true); statement.

Given the complexity of the multivariable array structure, I’d like to dive down and pull out a specific value if it exists. An example to help explain.

Here is the json data abreviated to save space:

{
“results” : [
{
“address_components” : [
{
“long_name” : “2917-2981”,
“short_name” : “2917-2981”,
“types” : [ “street_number” ]
},
{
“long_name” : “Runyon Canyon Rd”,
“short_name” : “Runyon Canyon Rd”,
“types” : [ “route” ]
},
{
“long_name” : “Hollywood Hills”,
“short_name” : “Hollywood Hills”,
“types” : [ “neighborhood”, “political” ]
},
{
“long_name” : “Los Angeles”,
“short_name” : “Los Angeles”,
“types” : [ “locality”, “political” ]
}
….

my code:

…
$data = @file_get_contents($url);
$phpresult = json_decode($data,true);
var_dump($phpresult);

var_dump($phpresult) displays:

array(2) { [“results”]=> array(8) { [0]=> array(4) { [“address_components”]=> array(8) { [0]=> array(3) { [“long_name”]=> string(9) “2917-2981” [“short_name”]=> string(9) “2917-2981” [“types”]=> array(1) { [0]=> string(13) “street_number” } } [1]=> array(3) { [“long_name”]=> string(16) “Runyon Canyon Rd” [“short_name”]=> string(16) “Runyon Canyon Rd” [“types”]=> array(1) { [0]=> string(5) “route” } } [2]=> array(3) { [“long_name”]=> string(15) “Hollywood Hills” [“short_name”]=> string(15) “Hollywood Hills” [“types”]=> array(2) { [0]=> string(12) “neighborhood” [1]=> string(9) “political” } } [3]=> array(3) { [“long_name”]=> string(11) “Los Angeles” [“short_name”]=> string(11) “Los Angeles” [“types”]=> array(2) { [0]=> string(8) “locality” [1]=> string(9) “political” }…<

I’d like to store the value “Los Angeles” in variable $locality by searching/parsing/? the array for the condition “type = locality”.

I do not want find the information by using the code:

echo $phpresult[‘results’][0][‘address_components’][3][‘long_name’];

because that assumes the variable is always 1) in the same location within the array and 2) that it exists.

Any help would be really appreciated. I haven’t found an answer and have probably read (but not totally understood) every post relating to the subject.

Thanks in advance for your help.

Shaun

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:58:24+00:00Added an answer on June 9, 2026 at 4:58 am
    foreach($phpresult['results'][0]['address_components'] as $location)
    {
        if(in_array("locality" , $location['types']))
        {
            $locality[] = $location['long_name'];
        }
    }
    
    print_r($locality);
    

    The array of each location has 3 keys: long_name , short_name and types.
    Types is an array , so , if there’s a value of “locality” in that types array
    get the long_name of that location and store it.

    EDIT: This function will store all the long_names of location which has a locality type,
    not just the first one.
    In order to get only the first location’s long_name ,add “break” after the $locality[] = ....

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm connecting to the Google Maps API from PHP to geocode some starting points
hi when i write css or html i found that i want add two
I have a list of string values that I want add to a hashtable
I have problem, when I want add Node to my GUI from other Thread.
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
What should I do, if I want add routing rules from database. Should I
I want add days to an NSDate . The approach below is not working.
I have a report that I want add a line beneath the even rows
I want add some methods. And this directive can't help me. var viewModel =
I want add two textview with orientation vertical(One below other) in a table row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.