How to find the depth of an unlimited depthed arra

2019-09-25 10:14发布

问题:

This question already has an answer here:

  • Is there a way to find out how “deep” a PHP array is? 18 answers

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

回答1:

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;
            }


回答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;

?>


回答3:

<?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.