title: Recursion in php, hierarchical structure in array, print tree
body: <p>i have a tree array like this with parent->child structure: </p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 12
              [address] =&gt; root addr           
              [parent_id] =&gt; 0
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [id] =&gt; 11
                              [address] =&gt; child addr                  
                              [parent_id] =&gt; 12
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [id] =&gt; 16
                                              [address] =&gt; child addr 2
                                              [parent_id] =&gt; 11
                                              [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [id] =&gt; 19
                                                              [address] =&gt; child addr 3 
                                                              [parent_id] =&gt; 16
                                                          )
  
                                                  )
  
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [id] =&gt; 18
                                              [address] =&gt; child addr 4
                                              [parent_id] =&gt; 11
                                          )
  
                                      [2] =&gt; Array
                                          (
                                              [id] =&gt; 20
                                              [address] =&gt; child addr 5
                                              [parent_id] =&gt; 11
                                          )
  
                                  )
  
                          )
  
                  )
  
          )
  
      [1] =&gt; Array
          (
              [id] =&gt; 17
              [address] =&gt; root addr 2        
              [parent_id] =&gt; 0
          ) 
  
  )
  </code></pre>
  
  <p>i have a build tree function, that print my tree structure :</p>
  
  <pre><code>function printTree($data, $level = 0, $p_counter = 1) {    
  
              foreach ($data as $item) {  
  
                  if ($item['parent_id'] == 0) {
                      $addr =  $p_counter . '. ' . $item['address'];
                      $p_counter++;
                  }
  
                  else if ($item['parent_id'] != 0) {
  
                      $addr =  str_repeat(' - ', $level) . $item['address'];   
  
  
                  } else {
                       $addr = $item['address'];              
                  }
  
                  global $result;
  
                  $result['aaData'][] = Array(
                   $addr,
  
               );   
  
                  if (isset($item['children'])) {                    
                      printTree($item['children'], $level + 1, $p_counter + 1);
                  }
  
  
              }
  
              return $result;        
          }
  </code></pre>
  
  <p>Now my output is like this with childs levels </p>
  
  <pre><code>1. root addr  
  - child addr        
  - - child arr 2 
  - - - child arr 3        
  - - child addr 4     
  - - child addr 5     
  2. root addr 2
  </code></pre>
  
  <p>my goal is to print array like this, need help!</p>
  
  <pre><code>1. root addr  
  1-1. child addr        
  1-1-1. child arr 2 
  1-1-1-1. child arr 3        
  1-1-2. child addr 4     
  1-1-3. child addr 5     
  2. root addr 2 
  </code></pre>