title: Build an associative array from parent child relationship
body: <p>I've 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. The information is stored in my database like so:</p>
  
  <p><img src="https://i.stack.imgur.com/ZlmJS.jpg" alt="enter image description here"></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>
  
  <p><strong>NOTE:</strong> I've asked a similar question, but it was NOT to build an associative array. Since it was not like I originally asked I figured another thread was required.</p>