title: Using recursion to build navigation
body: <p>I'm building navigation for a site and for the life of me I can't figure out recursion. I have all my data stored via MySQL using this design:</p>
  
  <p><img src="https://i.stack.imgur.com/wJjMR.jpg" alt="enter image description here"></p>
  
  <p>I've read several links on how recursion works and I must be slow because it's difficult for me to grasp. I've tried to write something and I know it is not even close to what I really need, but it's a start:</p>
  
  <p>PDO</p>
  
  <pre><code>public function viewCategories()
  {
      $viewSQL = "SELECT * FROM categories";  
      try
      {
          $pdo = new PDO('mysql:host=localhost;dbname=store','root','');
          $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
          $categoryVIEW = $pdo-&gt;prepare($viewSQL);
          $categoryVIEW-&gt;execute();
          $array = $categoryVIEW-&gt;fetchAll(PDO::FETCH_ASSOC);
          $categoryVIEW-&gt;closeCursor();
          $json = json_encode($array);
          return $json;
      }
      catch(PDOexception $e)
      {
          return $e-&gt;getMessage();
          exit();
      }
  }
  </code></pre>
  
  <p>Recursion</p>
  
  <pre><code>$return = json_decode($category-&gt;viewCategories(),true);
  
  function buildNavigation($json)
  {
      foreach($json as $item)
      {
          if($item['category_id'] === $item['parent'])
          {
              print('&lt;li&gt;'.$item['category_name'].'&lt;/li&gt;');
              if($item['category_id'] === $item['parent'])
              {
                  print('&lt;li&gt;match'.$item['category_name'].'&lt;/li&gt;');
                  buildNavigation($json);
              }
          }
  }
  buildNavigation($return);
  </code></pre>
  
  <p>as expected this will never enter the condition. I did try to figure this out on my own since this is a good thing to have knowledge of, but I guess it's beyond my mental capacity :(</p>
  
  <p>Thanks for having a look :)</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I know this has been answered already, but is there a way I can do this to build an associative array? I have been playing around with a function that ALMOST works for me that I got from <strong><a href="https://stackoverflow.com/questions/3776798/php-array-recursion">HERE</a></strong>, but it adds an extra array that I do NOT want.</p>
  
  <p><strong>Method</strong></p>
  
  <pre><code>private function buildCategories($array,$parent)
  {
      $result = array();
      foreach($array as $row)
      {
          if($row['parent'] == $parent)
          {
              $result[$row['category_name']] = $this-&gt;buildCategories($array,$row['category_id']);
          }
      }
      return $result;
  }
  $json = json_encode($this-&gt;buildCategories($array,NULL));
  return $json;
  </code></pre>
  
  <p>I want this:</p>
  
  <pre><code>{"reloading":{"components","presses and dies","tumblers &amp; scales","tools &amp; accessories","shotshell reloading"}
  </code></pre>
  
  <p>but what I get is this:</p>
  
  <pre><code>{"reloading":{"components":[],"presses and dies":[],"tumblers &amp; scales":[],"tools &amp; accessories":[],"shotshell reloading":[]}
  </code></pre>