title: How to find the depth of an unlimited depthed array
body: <p>I am trying to find out the depth of a multi dimension array. The array is unlimited one (depth). I would like to find out the maximum depth of the array. Please see the array structure below</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 27
              [cata_key] =&gt; 55437c82626479a7b8554532
              [cata_name] =&gt; Road
              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
              [parentid] =&gt; 0
              [subcategories] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [id] =&gt; 28
                              [cata_key] =&gt; 6f031dbf1b3a641689277aee
                              [cata_name] =&gt; Four Wheelers
                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                              [parentid] =&gt; 27
                              [subcategories] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [id] =&gt; 29
                                              [cata_key] =&gt; b6c6ef585ba8e3618e05155e
                                              [cata_name] =&gt; Cars
                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                              [parentid] =&gt; 28
                                              [subcategories] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [id] =&gt; 30
                                                              [cata_key] =&gt; 6f3e2469551ad89c7f724b9f
                                                              [cata_name] =&gt; Hyundai
                                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                                              [parentid] =&gt; 29
                                                              [subcategories] =&gt; Array
                                                                  (
                                                                  )
  
                                                          )
  
                                                      [1] =&gt; Array
                                                          (
                                                              [id] =&gt; 31
                                                              [cata_key] =&gt; f2f345824d547e08121b6d54
                                                              [cata_name] =&gt; Honda
                                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                                              [parentid] =&gt; 29
                                                              [subcategories] =&gt; Array
                                                                  (
                                                                  )
  
                                                          )
  
                                                  )
  
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; Array
                          (
                              [id] =&gt; 32
                              [cata_key] =&gt; 74cec939a64cef188fb04458
                              [cata_name] =&gt; Two Wheelers
                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                              [parentid] =&gt; 27
                              [subcategories] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [id] =&gt; 33
                                              [cata_key] =&gt; 2c31a2bdb12458a4537f3c7a
                                              [cata_name] =&gt; Hero Honda
                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                              [parentid] =&gt; 32
                                              [subcategories] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [id] =&gt; 39
                                                              [cata_key] =&gt; e55b2b38218f055b01cd58aa
                                                              [cata_name] =&gt; Splender
                                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                                              [parentid] =&gt; 33
                                                              [subcategories] =&gt; Array
                                                                  (
                                                                  )
  
                                                          )
  
                                                  )
  
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [id] =&gt; 34
                                              [cata_key] =&gt; dbe8c85a694913a33c73acb9
                                              [cata_name] =&gt; Bajaj
                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                              [parentid] =&gt; 32
                                              [subcategories] =&gt; Array
                                                  (
                                                  )
  
                                          )
  
                                  )
  
                          )
  
                  )
  
          )
  
      [1] =&gt; Array
          (
              [id] =&gt; 35
              [cata_key] =&gt; 05f450e3fe710f186517e61a
              [cata_name] =&gt; River
              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
              [parentid] =&gt; 0
              [subcategories] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [id] =&gt; 36
                              [cata_key] =&gt; 8cc784f715135661c42733c2
                              [cata_name] =&gt; Boats
                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                              [parentid] =&gt; 35
                              [subcategories] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [id] =&gt; 37
                                              [cata_key] =&gt; 2668efe22245c3e0a897edf8
                                              [cata_name] =&gt; 2 seater
                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                              [parentid] =&gt; 36
                                              [subcategories] =&gt; Array
                                                  (
                                                  )
  
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [id] =&gt; 38
                                              [cata_key] =&gt; d90fd97291506cd823713543
                                              [cata_name] =&gt; 6 seater
                                              [app_key] =&gt; 3bb4f64af3d25034f0ae35bb
                                              [parentid] =&gt; 36
                                              [subcategories] =&gt; Array
                                                  (
                                                  )
  
                                          )
  
                                  )
  
                          )
  
                  )
  
          )
  
  )
  </code></pre>
  
  <p>Desired output is 4</p>
  
  <p>I have already tried</p>
  
  <pre><code>&lt;?php
  </code></pre>
  
  <p>function array_depth(array $array) {
      $max_depth = 1;</p>
  
  <pre><code>foreach ($array as $value) {
      if (is_array($value)) {
          $depth = array_depth($value) + 1;
  
          if ($depth &gt; $max_depth) {
              $max_depth = $depth;
          }
      }
  }
  
  return $max_depth;
  </code></pre>
  
  <p>}</p>
  
  <p>?></p>
  
  <p>But it is not working well</p>
  
  <p>Thanks in advance.</p>
  
  <p>Regards,</p>
  
  <p>Sunil</p>