Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Edit:</h2> <p>I had previously posted a solution to build a multi-dimensional array out of the output you gave as well as a way to get all child elements of a particular <code>id</code> out of that particular array. I have now figured out how to retrieve the child elements straight from your output (without having to first go through a <code>buildtree()</code> function:</p> <pre><code>function fetch_recursive($src_arr, $currentid, $parentfound = false, $cats = array()) { foreach($src_arr as $row) { if((!$parentfound &amp;&amp; $row['id'] == $currentid) || $row['parent_id'] == $currentid) { $rowdata = array(); foreach($row as $k =&gt; $v) $rowdata[$k] = $v; $cats[] = $rowdata; if($row['parent_id'] == $currentid) $cats = array_merge($cats, fetch_recursive($src_arr, $row['id'], true)); } } return $cats; } </code></pre> <p>To use the function above, simply pass in the output array <code>$data</code> to the first argument and the <code>id</code> you want to retrieve the child elements from in the second argument:</p> <p>ex.:</p> <pre><code>$list = fetch_recursive($data, 3); </code></pre> <p>Which should give you the correct array structure for <code>id</code> <code>3</code> (as seen in the example in the last codebox to this answer).</p> <hr> <p><em><strong>Original Answer:</em></strong></p> <p>I had never got around to writing a recursive function to build nested trees out of this design until now. I'm sure there are plenty of others who have written similar functions, but this one should definitely work for you:</p> <pre><code>function buildtree($src_arr, $parent_id = 0, $tree = array()) { foreach($src_arr as $idx =&gt; $row) { if($row['parent_id'] == $parent_id) { foreach($row as $k =&gt; $v) $tree[$row['id']][$k] = $v; unset($src_arr[$idx]); $tree[$row['id']]['children'] = buildtree($src_arr, $row['id']); } } ksort($tree); return $tree; } </code></pre> <p>This function will recursively build a tree out of an adjacency list and keep the id's ordered in ascending order. This also makes the <code>id</code>'s of each parent/child the key of each array of information.</p> <p>This code:</p> <pre><code>$r = mysql_query("SELECT * FROM test "); $data = array(); while($row = mysql_fetch_assoc($r)) { $data[] = $row; } echo '&lt;pre&gt;'; print_r(buildtree($data)); echo '&lt;/pre&gt;'; </code></pre> <p>Will output something like this:</p> <pre><code>Array ( [1] =&gt; Array ( [id] =&gt; 1 [name] =&gt; Electronics [parent_id] =&gt; 0 [children] =&gt; Array ( [2] =&gt; Array ( [id] =&gt; 2 [name] =&gt; Televisions [parent_id] =&gt; 1 [children] =&gt; Array ( [4] =&gt; Array ( [id] =&gt; 4 [name] =&gt; Tube [parent_id] =&gt; 2 [children] =&gt; Array() ) [5] =&gt; Array ( [id] =&gt; 5 [name] =&gt; LCD [parent_id] =&gt; 2 [children] =&gt; Array() ) [6] =&gt; Array ( [id] =&gt; 6 [name] =&gt; Plasma [parent_id] =&gt; 2 [children] =&gt; Array() ) ) ) [3] =&gt; Array ( [id] =&gt; 3 [name] =&gt; Portable Electronics [parent_id] =&gt; 1 [children] =&gt; Array ( [7] =&gt; Array ( [id] =&gt; 7 [name] =&gt; Mp3 Players [parent_id] =&gt; 3 [children] =&gt; Array ( [10] =&gt; Array ( [id] =&gt; 10 [name] =&gt; Flash [parent_id] =&gt; 7 [children] =&gt; Array() ) ) ) [8] =&gt; Array ( [id] =&gt; 8 [name] =&gt; CD Players [parent_id] =&gt; 3 [children] =&gt; Array() ) [9] =&gt; Array ( [id] =&gt; 9 [name] =&gt; 2 Way Radios [parent_id] =&gt; 3 [children] =&gt; Array() ) ) ) ) ) ) </code></pre> <p>To get all child-nodes of a particular <code>id</code> onto a one-dimensional array, you can use this function:</p> <pre><code>function fetch_recursive($tree, $parent_id, $parentfound = false, $list = array()) { foreach($tree as $k =&gt; $v) { if($parentfound || $k == $parent_id) { $rowdata = array(); foreach($v as $field =&gt; $value) if($field != 'children') $rowdata[$field] = $value; $list[] = $rowdata; if($v['children']) $list = array_merge($list, fetch_recursive($v['children'], $parent_id, true)); } elseif($v['children']) $list = array_merge($list, fetch_recursive($v['children'], $parent_id)); } return $list; } </code></pre> <p>Based on the <code>buildtree()</code> function above, let's say we wanted to get all child nodes of <code>id</code> 3:</p> <pre><code>echo '&lt;pre&gt;'; print_r(fetch_recursive(buildtree($a), 3)); echo '&lt;/pre&gt;'; </code></pre> <p>This will output:</p> <pre><code>Array ( [0] =&gt; Array ( [id] =&gt; 3 [name] =&gt; Portable Electronics [parent_id] =&gt; 1 ) [1] =&gt; Array ( [id] =&gt; 7 [name] =&gt; Mp3 Players [parent_id] =&gt; 3 ) [2] =&gt; Array ( [id] =&gt; 10 [name] =&gt; Flash [parent_id] =&gt; 7 ) [3] =&gt; Array ( [id] =&gt; 8 [name] =&gt; CD Players [parent_id] =&gt; 3 ) [4] =&gt; Array ( [id] =&gt; 9 [name] =&gt; 2 Way Radios [parent_id] =&gt; 3 ) ) </code></pre>
    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. 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