title: php array count parent/child
body: <p>I can't get it working right. What i want is that counter will count every parent and inside parent child that will look like table of content, result needs to be:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;ul&gt;
    &lt;li&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;  &lt;strong&gt;a&lt;/strong&gt;
    &lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;span style="color: red;"&gt;1.1&lt;/span&gt;  &lt;strong&gt;b&lt;/strong&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;span style="color: red;"&gt;1.2&lt;/span&gt;  &lt;strong&gt;c&lt;/strong&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;li&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt;  &lt;strong&gt;e&lt;/strong&gt;
    &lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;span style="color: red;"&gt;2.1&lt;/span&gt;  &lt;strong&gt;f&lt;/strong&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;span style="color: red;"&gt;2.2&lt;/span&gt;  &lt;strong&gt;g&lt;/strong&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;li&gt;&lt;span style="color: red;"&gt;3&lt;/span&gt;  &lt;strong&gt;h&lt;/strong&gt;
    &lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;span style="color: red;"&gt;3.1&lt;/span&gt;  &lt;strong&gt;k&lt;/strong&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;span style="color: red;"&gt;3.2&lt;/span&gt;  &lt;strong&gt;l&lt;/strong&gt;
      &lt;/li&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;span style="color: red;"&gt;3.2.1&lt;/span&gt;  &lt;strong&gt;m&lt;/strong&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;span style="color: red;"&gt;3.2.2&lt;/span&gt;  &lt;strong&gt;n&lt;/strong&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>There are n childs soo deep is unlimited..
  My current code is here:</p>
  
  <pre><code>&lt;?php 
  $arr = array(
      array('id' =&gt; 1, 'title' =&gt; 'a', 'parent' =&gt; 0),
      array('id' =&gt; 2, 'title' =&gt; 'b', 'parent' =&gt; 1),
      array('id' =&gt; 3, 'title' =&gt; 'c', 'parent' =&gt; 1), 
      array('id' =&gt; 5, 'title' =&gt; 'e', 'parent' =&gt; 0),
      array('id' =&gt; 6, 'title' =&gt; 'f', 'parent' =&gt; 5),
      array('id' =&gt; 7, 'title' =&gt; 'g', 'parent' =&gt; 5), 
      array('id' =&gt; 8, 'title' =&gt; 'h', 'parent' =&gt; 0),
      array('id' =&gt; 9, 'title' =&gt; 'k', 'parent' =&gt; 8),
      array('id' =&gt; 10, 'title' =&gt; 'l', 'parent' =&gt; 8), 
      array('id' =&gt; 11, 'title' =&gt; 'm', 'parent' =&gt; 10), 
      array('id' =&gt; 12, 'title' =&gt; 'n', 'parent' =&gt; 10), 
  );
  
  function checkChilds($arr, $id){
      foreach ($arr as $key =&gt; $value) {
          if ($value['parent'] == $id) {
              return true;
          }
      }
      return false;
  }
  
  function getList($arr, $parent = 0, $subcounter = 1){
      $output = null;
  
      $output .= '&lt;ul&gt;';
      $counterParent = 1;
      $countChild = 1;
      foreach($arr as $key =&gt; $value) {
          if($value['parent'] == $parent){
  
              $counter = ($value['parent'] == 0) ? "{$counterParent}" : "{$subcounter}.{$countChild}";
              $output .= "&lt;li&gt;&lt;span style='color: red;'&gt;{$counter}&lt;/span&gt; &lt;strong&gt;{$value['title']}&lt;/strong&gt;&lt;/li&gt;";
  
              if (checkChilds($arr, $value['id'])){
                  $output .= getList($arr, $value['id'], $counterParent);
                  $countChild++;
              }
  
              // counter for parent
              if($value['parent'] == 0) {
                  $counterParent++;
              }
          }
      }
      $output .= '&lt;/ul&gt;';
  
      return $output;
  }
  
  echo getList($arr);
  </code></pre>