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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:59:20+00:00 2026-05-30T00:59:20+00:00

I have a small application that does the following: allows the user to upload

  • 0

I have a small application that does the following:

  • allows the user to upload an XML file
  • parses the XML file into an array to work with $_SESSION
  • displays a list of parent element names the user can choose to view
  • parses the array to display children of the selected parent elements

The issue is that every item can have children who can have children who can have children… etc… and this can go on indefinitely.

How can you include every child of a child in the final output?

The XML files can look similar to this:

<thing>
  <parents>
    <parent>
      <name>parent 1</name>
      <categories>
        <category>
          <name>category 1</name>
          <items>
            <item>
              <name>item 1 (gen 1)</name>
              <items>
                <item>
                  <name>sub-item 1 (gen 2)</name>
                  <items>
                    <item>
                      <name>sub-sub-item 1 (gen 3)</name>
                      ...this could continue forever..
                    </item>
                  </items>
                </item>
              </items>
            </item>
          </items>
        </category>
      </categories>
    </parent>
  </parents>
</thing>

I’ve parsed the XML into an array with PHP’s SimpleXML. Every file must have a parent, category, and first generation child items. The following code parses through these 3 levels of structure, but beyond that I’m lost.

$output .= '<ul>';
foreach($xml['parents'] as $parent){
  $output .= '<li>'.$parent['name'].'</li>';
  foreach($parent['categories']['category'] as $category){
    $output .= '<li>'.$category['name'].'</li>';
    foreach($category['items']['item'] as $item){
      $output .= '<li>'.$item['name'].'</li>';
      // here is where the $item can have children w/ children
      // who can have children who can have children... etc... forever.
      // (i.e. $item['items']['item'] as $generation2_items++...)
      //
      // I want to add them as another <li></li>...
      //
      // How can you account for unknown # of children?
    }
  }
}
$output .= '</ul>';
echo $output;

The code $outputs a list similar to:

- parent 1
-- category 1
--- item 1 (gen 1)
---- sub item 1 (gen 2)
----- sub-sub item 1 (gen 3)
------- etc.
-------- etc.

How can you determine how many child elements deep each item goes and then how can you create enough loops to parse through accordingly… or iterate through another way?

Thanks for your help.

Solution

PHP recursion function solved it. Here’s what I used when I got to the possible infinite repeating part:

function foo($element, $indent=0){
  $result .= '<li>';
  if($indent > 0){
    for($i=1;$i<=$indent;$i++){
      $result .= '&nbsp;&nbsp;&nbsp;&nbsp;';
    }
  }
  $result .= $element['name'].'</li>';
  if(isset($element['children']['child'])){
    $i++;
    foreach($element['children']['child'] as $child){
   $result .= foo($child, $i);
    }
  }
  return $result;
}    

$output .= foo($item);
  • 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-30T00:59:22+00:00Added an answer on May 30, 2026 at 12:59 am

    You may use a recursive function for this. Every programmer should know how to use recursion; if you don’t: go ahead and learn it right away!

    What you basically want to do is to create a function, let’s call it foo(), which takes one item as it’s input. foo will do two things:

    1. Output the current item
    2. For each child, call itself with the child as input.

    Creating recursive functions is, as I said, really useful and you should both learn and exercise this tool. You may for example send a second argument to foo with the depth of the recursion so you can output the children with different indentation.

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

Sidebar

Related Questions

I have a small application that I am building a Chat application into, so
I have a small application that has a message only WTL window which does
I've been creating a small application that allows a user to convert images to
I have a small application that uses a SharePoint list as the data source.
I have a small application that redirects the stdout/in of an another app (usually
I have a small lightweight application that is used as part of a larger
I have a small application which embeds webbrowser controls. In that application I have
I have a small VB.NET application that I'm working on using the full version
I have a small application I am working on that at one point needs
I have a small AJAX application, written in PHP that I did not secure

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.