Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to include every child element of a child element in output? PHP
    primarykey
    data
    text
    <p>I have a small application that does the following:</p> <ul> <li>allows the user to upload an XML file</li> <li>parses the XML file into an array to work with $_SESSION</li> <li>displays a list of parent element names the user can choose to view</li> <li>parses the array to display children of the selected parent elements</li> </ul> <p>The issue is that every <em>item</em> can have children who can have children who can have children... etc... and this can go on indefinitely.</p> <p><strong>How can you include every child of a child in the final output?</strong></p> <p><strong>The XML files can look similar to this:</strong></p> <pre><code>&lt;thing&gt; &lt;parents&gt; &lt;parent&gt; &lt;name&gt;parent 1&lt;/name&gt; &lt;categories&gt; &lt;category&gt; &lt;name&gt;category 1&lt;/name&gt; &lt;items&gt; &lt;item&gt; &lt;name&gt;item 1 (gen 1)&lt;/name&gt; &lt;items&gt; &lt;item&gt; &lt;name&gt;sub-item 1 (gen 2)&lt;/name&gt; &lt;items&gt; &lt;item&gt; &lt;name&gt;sub-sub-item 1 (gen 3)&lt;/name&gt; ...this could continue forever.. &lt;/item&gt; &lt;/items&gt; &lt;/item&gt; &lt;/items&gt; &lt;/item&gt; &lt;/items&gt; &lt;/category&gt; &lt;/categories&gt; &lt;/parent&gt; &lt;/parents&gt; &lt;/thing&gt; </code></pre> <p>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.</p> <pre><code>$output .= '&lt;ul&gt;'; foreach($xml['parents'] as $parent){ $output .= '&lt;li&gt;'.$parent['name'].'&lt;/li&gt;'; foreach($parent['categories']['category'] as $category){ $output .= '&lt;li&gt;'.$category['name'].'&lt;/li&gt;'; foreach($category['items']['item'] as $item){ $output .= '&lt;li&gt;'.$item['name'].'&lt;/li&gt;'; // 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 &lt;li&gt;&lt;/li&gt;... // // How can you account for unknown # of children? } } } $output .= '&lt;/ul&gt;'; echo $output; </code></pre> <p>The code $outputs a list similar to:</p> <pre><code>- parent 1 -- category 1 --- item 1 (gen 1) ---- sub item 1 (gen 2) ----- sub-sub item 1 (gen 3) ------- etc. -------- etc. </code></pre> <p>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?</p> <p>Thanks for your help.</p> <p><strong>Solution</strong></p> <p>PHP recursion function solved it. Here's what I used when I got to the possible infinite repeating part:</p> <pre><code>function foo($element, $indent=0){ $result .= '&lt;li&gt;'; if($indent &gt; 0){ for($i=1;$i&lt;=$indent;$i++){ $result .= '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'; } } $result .= $element['name'].'&lt;/li&gt;'; if(isset($element['children']['child'])){ $i++; foreach($element['children']['child'] as $child){ $result .= foo($child, $i); } } return $result; } $output .= foo($item); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload