Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following code-example <em>converts</em> the array <code>$array</code> into the tree-structure you're looking for:</p> <pre><code>// key the array by id $keyed = array(); foreach($array as &amp;$value) { $keyed[$value['id']] = &amp;$value; } unset($value); $array = $keyed; unset($keyed); // tree it $tree = array(); foreach($array as &amp;$value) { if ($parent = $value['parent_id']) $array[$parent]['children'][] = &amp;$value; else $tree[] = &amp;$value; } unset($value); $array = $tree; unset($tree); var_dump($array); # your result </code></pre> <p>This does not work, if there is an <em>existing</em> parent id that is <code>0</code>. But could be easily changed to reflect that.</p> <p>This is a related question, that has the original array already keyed, so the first half of the solution could be spared: <a href="https://stackoverflow.com/questions/7673044/nested-array-third-level-is-disappearing/7673415">Nested array. Third level is disappearing</a>.</p> <p><strong>Edit:</strong></p> <p>So how does this work? This is making use of PHP variable aliasing (also known as <a href="http://www.php.net/manual/en/language.references.php" rel="nofollow noreferrer">references</a>) and (temporary) arrays that are used to store a) aliases to the nodes (<code>$keyed</code>) and b) to build the new tree order (<code>$tree</code>).</p> <blockquote> <p>Could you [...] explain the purpose of <code>$array = $keyed</code>, <code>$array = $tree</code> and the unsets?</p> </blockquote> <p>As both, <code>$keyed</code> and <code>$tree</code> contain references to values in <code>$array</code>, I first copy over that information into <code>$array</code>, e.g.:</p> <pre><code>$array = $keyed; </code></pre> <p>As now <code>$keyed</code> is still set (and contains references to the same values as in <code>$array</code>), <code>$keyed</code> is unset:</p> <pre><code>unset($keyed); </code></pre> <p>This un-sets all references in <code>$keyed</code> and ensures, that all values in <code>$array</code> aren't referenced any longer (the value's refcount is reduced by one).</p> <p>If the temporary arrays are not unset after the iteration, their references would still exist. If you use <code>var_dump</code> on <code>$array</code>, you would see that all values would have a <code>&amp;</code> in front, because they are still referenced. <code>unset($keyed)</code> removes these references, <code>var_dump($array)</code> again, and you will see the <code>&amp;</code>s are gone.</p> <p>I hope this was understandable, references can be hard to follow sometimes if you're not fluent with them. It often helps me to think about them as variable aliases.</p> <p>If you want some exercise, consider the following:</p> <blockquote> <p>How to convert your <code>$array</code> from flat to tree with one foreach iteration?</p> </blockquote> <p>Decide on your own when you would like to click the link which contains a <a href="http://codepad.viper-7.com/IUUDC4" rel="nofollow noreferrer">Solution</a>.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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