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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:18:22+00:00 2026-05-18T01:18:22+00:00

Hoping somebody can advise on an issue I have with simplexml. I need to

  • 0

Hoping somebody can advise on an issue I have with simplexml.

I need to specify the paths for various nodes but I’m not sure if this is possible.

$xml = new SimpleXMLElement($xmlstr);
$image1 = 'images->image[0]->image';

foreach ($xml->record as $record) {
  echo $record->$image1; // i need this be be recognised as $record->images->image[0]->image
}

Hope this makes sense! 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-05-18T01:18:22+00:00Added an answer on May 18, 2026 at 1:18 am

    You can use an array for this:

    $xml = new SimpleXMLElement($xmlstr);
    $levels = array('images', array('key' => 'image', 'index' => 0), 'image');
    
    foreach ($xml->record as $record) {
       $obj = $record;
       foreach($levels as $level) {
          if(is_array($level)) 
             $obj = $obj->{$level['key']}[$level['index']];
          else 
             $obj = $obj->$level; 
       }
       echo $obj;
    }
    

    This builds up the hierarchy by reassigning $obj equal to itself -> whatever is next in the array.

    PHP cannot interpolate array indices in strings, so if you need to use them, just use an associate array as shown above. 🙂

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

Sidebar

Related Questions

This could be a bit of a niche issue but I'm hoping somebody can
Have run into an issue that i am hoping somebody can help give me
I'm afraid I already know the answer, but I'm hoping that somebody can provide
Okay, this may be trivial, but I'm hoping somebody can give me a straightforward
I have a sorting/grouping issue that I'm hoping somebody could add some insight on.
I'm hoping somebody can clear up this issue I am having. None of the
Greetings all... I am hoping somebody can shed me some lights about the issue
I'm sure there isn't but I was hoping somebody could help me to work
So I'm hoping somebody can just explain to my why when I run the
I have an issue enabling functions with jQuery AutoComplete and Clone that I’m hoping

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.