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 7852753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:29:51+00:00 2026-06-02T19:29:51+00:00

I am trying to get the value inside the <address_component> element that corresponds to

  • 0

I am trying to get the value inside the <address_component> element that corresponds to <type>postal_code</type> from the XML returned by the Google maps REST API.

For example, in the below:

    ...

  <result>    
    <type>
      route
    </type>    
    <formatted_address>
      Rose Ln, Liverpool, Merseyside L18 5ED, UK
    </formatted_address>    
    <address_component>      
      <long_name>
        Rose Ln
      </long_name>      
      <short_name>
        Rose Ln
      </short_name>      
      <type>
        route
      </type>      
    </address_component>    
    <address_component>      
      <long_name>
        Liverpool
      </long_name>      
      <short_name>
        Liverpool
      </short_name>      
      <type>
        locality
      </type>      
      <type>
        political
      </type>      
    </address_component>    
    <address_component>      
     ...      
    </address_component>    
    <address_component>      
    ...
    </address_component>    
    <address_component>      
      <long_name>
        L18 5ED
      </long_name>      
      <short_name>
        L18 5ED
      </short_name>      
      <type>
        postal_code
      </type>      
    </address_component>    
    <address_component>      
    ...      
    </address_component>    
   ...    
  </result>

  ... more result elements

We see one of the <result> elements. Nested inside are multiple <address_component> elements. Inside one of those is <type>postal_code</type>.

My question is how would I distinguish between these same named elements and choose only the <address_component> with <type>postal_code</type> attributed to it?

If they were uniquely named it would be a simple case of:

foreach ($address_rsponse->result as $address_option) {
    $val = $address_option->some_unique_name;      
}

However when named the same I am stumped as to a method to pick out an element by it’s child-element.

Could anyone shed any light on the correct approach.

Thanks

  • 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-02T19:29:52+00:00Added an answer on June 2, 2026 at 7:29 pm

    You can use XPath with SimpleXML (docs).

    The following will give you an array containing the <address_component> element(s) that you’re looking for.

    $address_rsponse->result->xpath(
        'address_component[normalize-space(type)="postal_code"]')
    

    If you want to loop over the <result> elements, the following will give you an array containing the <address_component> element(s) that you’re looking for and output the first for each <result>.

    foreach ($address_rsponse->result as $result) {
        $postal_codes = $result->xpath('address_component[normalize-space(type)="postal_code"]');
        // Do whatever with the postal code(s)
        echo trim($postal_codes[0]->long_name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the value of variable a that is inside the
Hello Im trying to get the value of an element from a JsonObject. i'm
I am trying to get an attribute value from an XML file, but my
I'm trying to get a value from a variable inside some files, here is
Trying to get a value out of 2d array inside of a hash and
While i am trying to get value from arraylist,i got the following errors.My sample
I am trying to get the value of href of <a> that is the
I'm trying to get a value from an array used in the Joomla Plugin
I am trying to get an ID from a url parameter inside an href
I am trying to get the value of the text in the input fields.

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.