title: How can I display an array in a tree view structure?
body: <p>I want to build a tree view made of departments(parent) and sub-departments(child). In my database I think I have a good structure which is like this:</p>
  
  <pre><code>--------------------------------------------
   Dep_name         | dep_id | dep_parent_id    
  --------------------------------------------
   Accounting       |    1   |        0
   Human-Resources  |    2   |        0
   IT               |    3   |        0
   Network          |    4   |        3
   Web Development  |    5   |        3
   Front-End        |    6   |        5
   Back-End         |    7   |        5
  </code></pre>
  
  <p>Departments which have the dep_parent_id 0 ---> they do not have a parent department. For example, Web Development and Network are children of IT department. Front End and Back End are children of Web Development.</p>
  
  <p>I have found a recursive function which is appropriate to get all the data from this database table and put them on an array in te right structure. But the problem is that I dont know how to display this array like a tree view.
  Like this for example</p>
  
  <ul>
  <li>Departments
  
  <ul>
  <li>Accounting</li>
  <li>Human Resources</li>
  <li>IT</li>
  <li>Network</li>
  <li>Web Development
  
  <ul>
  <li>Front End</li>
  <li>Back End</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>..... and so on ....</p>
  
  <p>In my database I think I have a good structure which is like this:</p>
  
  <p><a href="https://i.stack.imgur.com/OwUmV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OwUmV.png" alt="enter image description here"></a></p>
  
  <p>I have tried to print the array in a very simple way using </p>
  
  <pre><code>print_r($tree);
  </code></pre>
  
  <p>And it prints it like this:</p>
  
  <pre><code>Array (
      [0] =&gt; stdClass Object (
          [Dep_name] =&gt; Accounting and Finance
          [dep_id] =&gt; 1
          [dep_parent_id] =&gt; 0
      )
      [1] =&gt; stdClass Object (
          [Dep_name] =&gt; Human-Recources
          [dep_id] =&gt; 2
          [dep_parent_id] =&gt; 0
      )
      [2] =&gt; stdClass Object (
          [Dep_name] =&gt; IT
          [dep_id] =&gt; 3
          [dep_parent_id] =&gt; 0
          [children] =&gt; Array (
              [0] =&gt; stdClass Object (
                  [Dep_name] =&gt; Network
                  [dep_id] =&gt; 5
                  [dep_parent_id] =&gt; 3
              )
              [1] =&gt; stdClass Object (
                  [Dep_name] =&gt; Web Development
                  [dep_id] =&gt; 6
                  [dep_parent_id] =&gt; 3
                  [children] =&gt; Array (
                      [0] =&gt; stdClass Object (
                          [Dep_name] =&gt; Front-End
                          [dep_id] =&gt; 7
                          [dep_parent_id] =&gt; 6
                      )
                      [1] =&gt; stdClass Object (
                          [Dep_name] =&gt; Back-End
                          [dep_id] =&gt; 8
                          [dep_parent_id] =&gt; 6
                      )
                  )
              )
          )
      )
      [3] =&gt; stdClass Object (
          [Dep_name] =&gt; Marketing
          [dep_id] =&gt; 4
          [dep_parent_id] =&gt; 0
          [children] =&gt; Array (
              [0] =&gt; stdClass Object (
                  [Dep_name] =&gt; web-marketing
                  [dep_id] =&gt; 9
                  [dep_parent_id] =&gt; 4
              )
          )
      )
  )
  </code></pre>
  
  <p>This is my function that gets data from database table from array $data and builds a tree array $branch.</p>
  
  <pre><code>function buildTree(array $data, $parentId = 0) 
  {
      $branch = array();
  
      foreach ($data as $element) 
      {
          if ($element-&gt;dep_parent_id == $parentId) 
          {
              $children = buildTree($data, $element-&gt;dep_id);
              if ($children) 
              {
                  $element-&gt;children = $children;
              }
              $branch[] = $element;
          }
  
      }
  
      return $branch;
  }
  </code></pre>
  
  <p>And than I print it using :</p>
  
  <pre><code>print_r(buildTree($data));
  </code></pre>
  
  <p>I would be very grateful if you would help me to solve this and display a tree view structure in html from array $branch that I return from function <code>buildTree($data)</code>.</p>