Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't quite tell from your post, but is the code you posted is the code you have, and the output you posted is the output you want? The code you posted... I want to say won't, but in truth I don't know php well enough to be sure... shouldn't produce the output you posted. In fact, I'd be shocked if it produced any output at all. The array's you posted have key-value pairs. None of the keys are anything like 'parentDiv', 'childDiv', 'grandChildDiv' or 'greatGrandChildDiv'. Instead they are just 'attribute_value' for the first one and for the second one 'attribute_value' and 'parent_container_name' for the second one. </p> <p>In order to generate the output you want, try something along these lines:</p> <pre><code>$containerWasGenerated = new array(); echo '&lt;div id="siteContainer"&gt;'; $containerWasGenerated['siteContainer'] = true; foreach($containers as $container) { if($containerWasGenerated[$container['attribute_value'] != true) { generateSubcontainers($container, $containers, $subcontainers, $containerWasGenerated); $containerWasGenerated[$container['attribute_value']] = true; } } echo '&lt;/div&gt;'; function generateSubcontainers($parent, $containers, $subcontainers, $containerWasGenerated) { echo '&lt;div id="'.$parent['attribute_value'].'"&gt;'; foreach($containers as $subcontainer) { if(getParent($subcontainer, $subcontainers) == $parent['attribute_value']) { generateContainer($subcontainer, $containers, $subcontainers, $containerWasGenerated); $containerWasGenerated[$subcontainer['attribute_value']] = true; } } echo '&lt;/div&gt;'; } function getParent($container, $subcontainers) { foreach($subconainters as $subcontainer) { if($subcontainer['attribute_value'] == $container['attribute_value']) { return $subcontainer['parent_container_name']; } } } </code></pre> <p>Disclaimer: this code is untested and my php is a little rusty, so it may be buggy as all heck. But it should be in the right direction. Also, it's still pretty inefficient. One way to speed it up: keep the subcontainers array ordered by parent_container_name and write a better getParent() function (using a binary search for instance). Hopefully this helps.</p>
 

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