title: How to make tree from child of parent categories?
body: <p>I have a problem when i want to create tree structure in my data this is my data for sample </p>
  
  <pre><code>$categories = array(
      array(
          'id' =&gt; 2 ,
          'name' =&gt; 'banana',
          'parent' =&gt; 1
      ),
  
      array(
          'id' =&gt; 1 ,
          'name' =&gt; 'fruit',
          'parent' =&gt; 0
      ),
  
      array(
          'id' =&gt; 3 ,
          'name' =&gt; 'Monkey',
          'parent' =&gt; 4
      ),
  
      array(
          'id' =&gt; 4 ,
          'name' =&gt; 'Animal',
          'parent' =&gt; 0
      ),
  
      array(
          'id' =&gt; 5 ,
          'name' =&gt; 'apple',
          'parent' =&gt; 1
      ),
  
      array(
          'id' =&gt; 6 ,
          'name' =&gt; 'Cat',
          'parent' =&gt; 4
      ),
  
      array(
          'id' =&gt; 7 ,
          'name' =&gt; 'Animal Water',
          'parent' =&gt; 1
      ),
  
      array(
          'id' =&gt; 8 ,
          'name' =&gt; 'fish',
          'parent' =&gt; 7
      ),
      array(
          'id' =&gt; 9 ,
          'name' =&gt; 'frog',
          'parent' =&gt; 7
      )
  );
  </code></pre>
  
  <p>then I want to restructure that array to be like this</p>
  
  <pre><code>$fix = array(
  
      array(
          'id' =&gt; 4 ,
          'name' =&gt; 'Animal',
          'parent' =&gt; 0,
          'child' =&gt; array(
              array(
                  'id' =&gt; 7 ,
                  'name' =&gt; 'Animal Water',
                  'parent' =&gt; 1,
                  'child' =&gt; array(
                      array(
                          'id' =&gt; 8 ,
                          'name' =&gt; 'fish',
                          'parent' =&gt; 7
                      ),
                      array(
                          'id' =&gt; 9 ,
                          'name' =&gt; 'frog',
                          'parent' =&gt; 7
                      )
                  )
              ),
  
              array(
                  'id' =&gt; 3 ,
                  'name' =&gt; 'Monkey',
                  'parent' =&gt; 4
              ),
  
              array(
                  'id' =&gt; 6 ,
                  'name' =&gt; 'Cat',
                  'parent' =&gt; 4
              ),
          )
      ),
  
      array(
          'id' =&gt; 1 ,
          'name' =&gt; 'fruit',
          'parent' =&gt; 0,
          'child' =&gt; array(
              array(
                  'id' =&gt; 2 ,
                  'name' =&gt; 'banana',
                  'parent' =&gt; 1
              ),
  
  
  
              array(
                  'id' =&gt; 5 ,
                  'name' =&gt; 'apple',
                  'parent' =&gt; 1
              )
          )
      )
  
  );
  </code></pre>
  
  <p>so I tried using this function , but not helped me out.</p>
  
  <pre><code>$GLOBALS['viyancs'] = array();
  $GLOBALS['i'] = 0;
  function finding_parent($categories) {
  
      foreach ($categories as $value) {
  
          //finding parent
          if ($value['parent'] === 0 ) {
              $GLOBALS['viyancs'][$GLOBALS['i']][$value['id']]= $value;
              $GLOBALS['i']++;
              continue;
          }
  
          if (isset($GLOBALS['viyancs'][$GLOBALS['i']][$value['parent']])) {
              $GLOBALS['viyancs'][$GLOBALS['i']][$value['parent']]['child']= $value;
          }
          $GLOBALS['i']++;
      }
      return $GLOBALS['viyancs'];
  }
  
  var_dump(finding_parent($categories));
  </code></pre>
  
  <p>any other suggestion ?</p>