Quick actions

cmd+k|ctrl+k

Navigation

Languages

4.1

Snippet info

Language

Php

Visibility

public

Author

sergiienko.d.g

Created

2017-03-06T13:51:32Z

Updated

2017-03-09T14:06:21Z

<?php

$treeData = [
    [11, 12, 13, 14], [
        21, 22, [
            231, 232, 234, 235, [
                2361, 2362, 2363
            ]
        ]
    ],
        [31, 32, [
            331, 332, 333
        ]
    ]
];

function depth($tree)
{
    $maxDepth = 0;
    $depth = -1;
    
    $tree = str_split(preg_replace('/[^\{\}]+/', '', serialize($tree)));
    
    foreach ($tree as $char) {
        $char == '{' ? $depth++ : $depth--;
        $maxDepth = max($maxDepth, $depth);
    }
    
    return $maxDepth;
}

echo depth($treeData);

INFO