Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First thing that comes to mind is just a flat version of what you have there:</p> <pre><code>array ( [0] =&gt; array( 'name' =&gt; 'item1', 'parent' =&gt; null ), [1] =&gt; array( 'name' =&gt; 'item2', 'parent' =&gt; null ), [3] =&gt; array( 'name' =&gt; 'item3', 'parent' =&gt; 0 ), [4] =&gt; array( 'name' =&gt; 'item4', 'parent' =&gt; 3 ), [5] =&gt; array( 'name' =&gt; 'item5', 'parent' =&gt; 1 ), [6] =&gt; array( 'name' =&gt; 'item6', 'parent' =&gt; 1 ), ); </code></pre> <p>Basically, you only ever reference back to the parent. To find all the children, you'd have to loop through the array. The initial setup time would be pretty quick, though.</p> <p>The second one that comes to mind, and would involve a lot more setup, but a lot less access time later on:</p> <pre><code>array ( [0] =&gt; array( 'name' =&gt; 'item1', 'parent' =&gt; null, 'children' = array(3) ), [1] =&gt; array( 'name' =&gt; 'item2', 'parent' =&gt; null 'children' = array(5, 6) ), [3] =&gt; array( 'name' =&gt; 'item3', 'parent' =&gt; 0 'children' = array(4) ), [4] =&gt; array( 'name' =&gt; 'item4', 'parent' =&gt; 3 'children' = array() ), [5] =&gt; array( 'name' =&gt; 'item5', 'parent' =&gt; 1 'children' = array() ), [6] =&gt; array( 'name' =&gt; 'item6', 'parent' =&gt; 1 'children' = array() ), ); </code></pre> <p>In this one, you'd be adding all the child indexes to the parent. It would take a little bit longer, but subsequent access times would be quick. As you're figuring out the parents, you simply append to the parent's children array.</p> <p>The only real downside to the second approach is that if you want to add or remove an item, you have to remember to go back and update the children array for the parent.</p>
    singulars
    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.
    1. VO
      singulars
      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