-3

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

Array
(
    [0] => Array
        (
            [id] => 27
            [cata_key] => 55437c82626479a7b8554532
            [cata_name] => Road
            [app_key] => 3bb4f64af3d25034f0ae35bb
            [parentid] => 0
            [subcategories] => Array
                (
                    [0] => Array
                        (
                            [id] => 28
                            [cata_key] => 6f031dbf1b3a641689277aee
                            [cata_name] => Four Wheelers
                            [app_key] => 3bb4f64af3d25034f0ae35bb
                            [parentid] => 27
                            [subcategories] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 29
                                            [cata_key] => b6c6ef585ba8e3618e05155e
                                            [cata_name] => Cars
                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                            [parentid] => 28
                                            [subcategories] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 30
                                                            [cata_key] => 6f3e2469551ad89c7f724b9f
                                                            [cata_name] => Hyundai
                                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                                            [parentid] => 29
                                                            [subcategories] => Array
                                                                (
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 31
                                                            [cata_key] => f2f345824d547e08121b6d54
                                                            [cata_name] => Honda
                                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                                            [parentid] => 29
                                                            [subcategories] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 32
                            [cata_key] => 74cec939a64cef188fb04458
                            [cata_name] => Two Wheelers
                            [app_key] => 3bb4f64af3d25034f0ae35bb
                            [parentid] => 27
                            [subcategories] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 33
                                            [cata_key] => 2c31a2bdb12458a4537f3c7a
                                            [cata_name] => Hero Honda
                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                            [parentid] => 32
                                            [subcategories] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 39
                                                            [cata_key] => e55b2b38218f055b01cd58aa
                                                            [cata_name] => Splender
                                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                                            [parentid] => 33
                                                            [subcategories] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 34
                                            [cata_key] => dbe8c85a694913a33c73acb9
                                            [cata_name] => Bajaj
                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                            [parentid] => 32
                                            [subcategories] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 35
            [cata_key] => 05f450e3fe710f186517e61a
            [cata_name] => River
            [app_key] => 3bb4f64af3d25034f0ae35bb
            [parentid] => 0
            [subcategories] => Array
                (
                    [0] => Array
                        (
                            [id] => 36
                            [cata_key] => 8cc784f715135661c42733c2
                            [cata_name] => Boats
                            [app_key] => 3bb4f64af3d25034f0ae35bb
                            [parentid] => 35
                            [subcategories] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 37
                                            [cata_key] => 2668efe22245c3e0a897edf8
                                            [cata_name] => 2 seater
                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                            [parentid] => 36
                                            [subcategories] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 38
                                            [cata_key] => d90fd97291506cd823713543
                                            [cata_name] => 6 seater
                                            [app_key] => 3bb4f64af3d25034f0ae35bb
                                            [parentid] => 36
                                            [subcategories] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

Desired output is 4

I have already tried

<?php

function array_depth(array $array) { $max_depth = 1;

foreach ($array as $value) {
    if (is_array($value)) {
        $depth = array_depth($value) + 1;

        if ($depth > $max_depth) {
            $max_depth = $depth;
        }
    }
}

return $max_depth;

}

?>

But it is not working well

Thanks in advance.

Regards,

Sunil

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
SunilKumar
  • 17
  • 1
  • 6

3 Answers3

2

This would be my take on it:

<?php

$arr = array(
    'a' => array(
        'b' => array(
            'c' => array(
                'd' => array(
                    'e' => array()//5 deep
                 )
            ),
            'c2' => array(
                'a' => array(
                    'b' => array(
                        'c' => array(
                            'd' => array()//7 deep
                        )
                    )
                )
            )
        )
    )
);

function get_array_depth($arr, $n = 0) {
    $max = $n;
    foreach ($arr as $item) {
        if (is_array($item)) {
            $max = max($max, get_array_depth($item, $n + 1));
        }
    }
    return $max;
}

$depth = get_array_depth($arr);
echo $depth;

?>
astupidname
  • 1,837
  • 15
  • 11
0
<?php
    function array_depth(array $array) {
        $max_depth = 1;
        foreach ($array as $value) {
            if (is_array($value)) {
                $depth = array_depth($value) + 1;
                if ($depth > $max_depth) {
                    $max_depth = $depth;
                }
            }
        }
        return $max_depth;
    }

    echo array_depth(array(array(array('a' => 1))));
 ?>

This yields 3. (Using your code - just reformatted.)

BTW: consider putting in some kind of safety against cyclic structures.

mzedeler
  • 4,177
  • 4
  • 28
  • 41
  • could u plz check using the array format I have posted in the question. It is not giving the correct number – SunilKumar Apr 22 '13 at 14:26
  • Why don't you just reformat your output to PHP arrays and try it yourself? – mzedeler Apr 22 '13 at 14:27
  • I have tried, but it is not giving the correct no – SunilKumar Apr 22 '13 at 14:34
  • for this array it is giving no output Array ( [0] => Array ( [id] => 42 [cata_key] => 3da35598dfe8b788ef6b8eef [cata_name] => Break Fast [app_key] => d0a1268a52b521adc3819e29 [parentid] => 0 [subcategories] => Array ( ) ) ) – SunilKumar Apr 22 '13 at 14:34
  • But that isn't PHP syntax. You can't write keys in brackets like ``[0] => ...``. Take a look at the manual: http://php.net/manual/en/function.array.php – mzedeler Apr 22 '13 at 17:45
-2

Try this man

                function array_depth($array, $n = 0) {
                    $max_depth = 1;
                    foreach ($array as $value) {
                        if (isset($value['subcategories'][0])) {
                            $depth = $this -> array_depth($value['subcategories']) + 1;
                            if ($depth > $max_depth) {
                                $max_depth = $depth;
                            }
                        }
                    }
                    return $max_depth;
            }
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
JLM
  • 100
  • 2
  • 10
  • @SunilKumar & JLM Comments complaining about the users are completely off-topic and inappropriate here. If you have a problem with the site or it's users that you want to discuss, post on Meta. If you have a problem you want handled by mods, Flag us. – Andrew Barber Apr 23 '13 at 01:47