title: How to limit array depth in php
body: <p>I want to get up to 10 level in multidimensional array. </p>
  
  <p>Here is my code from which i get up-to infinite depth, but i want to limit this to 10 levels.</p>
  
  <pre><code>function tree($parent){
  global $db;
  $sql = "SELECT * FROM clients WHERE parent_id = $parent ";
  
  
  $users=$db-&gt;query($sql);
  
  $return = array();
  
  while($parent = $users-&gt;fetchObject()) {
  
    $return[$parent-&gt;client_id] = $parent;
  
  
    if(have_data($parent-&gt;client_id)){
  
      $return[$parent-&gt;client_id]-&gt;subs = tree($parent-&gt;client_id);
    }
  }
  
  return $return;
  }
  </code></pre>
  
  <p>print_r(tree(1,0));</p>
  
  <p>the output is:</p>
  
  <pre><code>  Array
      (
      [2] =&gt; stdClass Object
      (
          [client_id] =&gt; 2
          [parent_id] =&gt; 1
          [client_name] =&gt; TEST
          [subs] =&gt; Array
              (
                  [4] =&gt; stdClass Object
                      (
                          [client_id] =&gt; 4
                          [parent_id] =&gt; 2
                          [client_name] =&gt; TEST
                          [subs] =&gt; Array
                              (
                                  [5] =&gt; stdClass Object
                                      (
                                          [client_id] =&gt; 5
                                          [parent_id] =&gt; 4
                                          [client_name] =&gt; ADAm
                                      )
  
                              )
  
                      )
  
                  [6] =&gt; stdClass Object
                      (
                          [client_id] =&gt; 6
                          [parent_id] =&gt; 2
                          [client_name] =&gt; RAS
                          [subs] =&gt; Array
                              (
                                  [7] =&gt; stdClass Object
                                      (
                                          [client_id] =&gt; 7
                                          [parent_id] =&gt; 6
                                          [client_name] =&gt; RAStest
                                      )
  
                              )
  
                      )
  
              )
  
      )
  
  [3] =&gt; stdClass Object
      (
          [client_id] =&gt; 3
          [parent_id] =&gt; 1
          [client_name] =&gt; ABC
      )
  
    )
  </code></pre>
  
  <p>How can i limit the child level to 10, Hope to get response from the experts boss.Thanks in advance.</p>