Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I replace multiple foreach loops with a 'for' loop or a recursive function?
    primarykey
    data
    text
    <p>I am stuck with a bunch of foreach loops and would like to know if there is a way to hone them down to a simple 'for' loop or a recursive function? I'm trying to generate HTML with the elements nested inside each other. I guess what I'm trying to get at is an arrays of arrays. But I don't know how to move forward with what I've created so far. Can someone please help me to make this monster into something more tamable? Thank you!</p> <p>Here's my code:</p> <pre><code>$containers = DISPLAY::displayParentElements($data); $subcontainers = DISPLAY::displayChildElements($data2); foreach($containers as $parent) { $parentDiv = $parent['parentDiv']; echo '&lt;div id="'.$parentDiv.'"&gt;'; foreach($subcontainers as $child) { echo '&lt;div id="'.$child['childDiv'].'"&gt;'; foreach($subcontainers as $grandChild) { echo '&lt;div id="'.$grandChild['grandChildDiv'].'"&gt;'; foreach($subcontainers as $greatGrandChild) { echo '&lt;div id="'.$greatGrandChild['greatGrandChildDiv'].'"&gt;'; echo '&lt;/div&gt;'; } echo '&lt;/div&gt;'; } echo '&lt;/div&gt;'; } echo '&lt;/div&gt;'; } </code></pre> <hr> <p>The results will then look like this:</p> <pre><code>&lt;div id="siteContainer"&gt; &lt;div id="header"&gt; &lt;div id="logoContainer"&gt;/div&gt; &lt;div id="logo"&gt;&lt;/div&gt; &lt;div id="links"&gt;&lt;/div&gt; &lt;div id="contactInfo"&gt; &lt;div id="logoText"&gt; &lt;div id="shortDiv"&gt; &lt;div class="headerText"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="body"&gt; &lt;div id="longDiv"&gt;&lt;/div&gt; &lt;div id="greetings"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div&gt; </code></pre> <hr> <p>The <code>$containers</code> array has the following info:</p> <pre><code>Array ( [attribute_value] =&gt; siteContainer ) Array ( [attribute_value] =&gt; header ) Array ( [attribute_value] =&gt; logoContainer ) Array ( [attribute_value] =&gt; logo ) Array ( [attribute_value] =&gt; logoText ) Array ( [attribute_value] =&gt; links ) Array ( [attribute_value] =&gt; contactInfo ) Array ( [attribute_value] =&gt; body ) Array ( [attribute_value] =&gt; longDiv ) Array ( [attribute_value] =&gt; shortDiv ) Array ( [attribute_value] =&gt; headerText ) Array ( [attribute_value] =&gt; greetings ) </code></pre> <hr> <p>The <code>$subcontainers</code> array has pretty much the same info but with an extra key:</p> <pre><code>Array ( [parent_container_name] =&gt; siteContainer [attribute_value] =&gt; header ) Array ( [parent_container_name] =&gt; header [attribute_value] =&gt; logoContainer ) Array ( [parent_container_name] =&gt; header [attribute_value] =&gt; logo ) Array ( [parent_container_name] =&gt; contactInfo [attribute_value] =&gt; logoText ) Array ( [parent_container_name] =&gt; header [attribute_value] =&gt; links ) Array ( [parent_container_name] =&gt; header [attribute_value] =&gt; contactInfo ) Array ( [parent_container_name] =&gt; siteContainer [attribute_value] =&gt; body ) Array ( [parent_container_name] =&gt; body [attribute_value] =&gt; longDiv ) Array ( [parent_container_name] =&gt; logoText [attribute_value] =&gt; shortDiv ) Array ( [parent_container_name] =&gt; shortDiv [attribute_value] =&gt; headerText ) Array ( [parent_container_name] =&gt; body [attribute_value] =&gt; greetings ) </code></pre> <p>I'm pretty sure the two arrays could be narrowed down into one or by only using the <code>$containers</code> array.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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