Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Create a multi-dimensional object from a flat dataset (from Mysql Table)
    text
    copied!<p>Here's the dataset in the mysql table:</p> <p>Using the MySQL Nested Set model, it's not obvious, because I have left out the <code>lft</code> and <code>rgt</code> columns.</p> <pre><code>+------+----------+--------------------------------+-------+ + Id | ParentId | Name | Level | +------+----------+--------------------------------+-------+ | 1001 | NULL | Computing | 0 | | 1002 | NULL | Cassettes &amp; Vinyl | 0 | | 1003 | 1001 | CD Players | 1 | | 1004 | 1002 | CD Writers | 1 | | 1005 | 1003 | CD-ROM Drives | 2 | | 1006 | 1004 | CDs | 2 | +------+----------+--------------------------------+-------+ </code></pre> <p>This is pulled into a 2 dimensional array:</p> <pre><code>&lt;?php $data = array( array('id' =&gt; 1001, 'ParentId' =&gt; NULL, 'Name' =&gt; 'Computing', 'Level' =&gt; 0), array('id' =&gt; 1002, 'ParentId' =&gt; NULL, 'Name' =&gt; 'Cassettes &amp; Vinyl', 'Level' =&gt; 0), array('id' =&gt; 1003, 'ParentId' =&gt; 1001, 'Name' =&gt; 'CD Players', 'Level' =&gt; 1), array('id' =&gt; 1004, 'ParentId' =&gt; 1002, 'Name' =&gt; 'CD Writers', 'Level' =&gt; 1), array('id' =&gt; 1005, 'ParentId' =&gt; 1003, 'Name' =&gt; 'CD-ROM Drives', 'Level' =&gt; 2), array('id' =&gt; 1006, 'ParentId' =&gt; 1004, 'Name' =&gt; 'Computing', 'Level' =&gt; 3) ); ?&gt; </code></pre> <p>I am trying to achieve the following multi-dimensional object:</p> <pre><code>stdClass Object { [0] =&gt; stdClass Object { [id] =&gt; 1001 [name] =&gt; Computing [parentId] =&gt; NULL [level] =&gt; 0 [children] =&gt; stdClass Object { [0] =&gt; stdClass Object { [id] =&gt; 1003 [name] =&gt; CD Players [parentId] =&gt; 1001 [level] =&gt; 1 [children] =&gt; stdClass Object { [0] =&gt; stdClass Object { [id] =&gt; 1005 [name] =&gt; CD Rom Drives [parentId] =&gt; 1003 [level] =&gt; 2 } } } } } [1] =&gt; stdClass Object { [id] =&gt; 1002 [name] =&gt; Cassettes &amp; Vinyl [parentId] =&gt; NULL [level] =&gt; 0 [children] =&gt; stdClass Object { [0] =&gt; stdClass Object { [id] =&gt; 1004 [name] =&gt; CD Writers [parentId] =&gt; 1002 [level] =&gt; 1 [children] =&gt; stdClass Object { [0] =&gt; stdClass Object { [id] =&gt; 1006 [name] =&gt; CDs [parentId] =&gt; 1004 [level] =&gt; 2 } } } } } } </code></pre> <p>I have been playing with a recursive function with little luck.</p> <p>Why? - This object will be used to create for Zend_Navigation. I do not want to use an XML file at this stage, I prefer the taxonomy management to be done in the database.</p> <p>Any ideas?</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