Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取php数组的最低级别_Php_Prestashop - Fatal编程技术网

获取php数组的最低级别

获取php数组的最低级别,php,prestashop,Php,Prestashop,我从prestashop得到了这个数组: [11]=> [id_category] => 11 [children] => [12]=> [id_category] => 12 [children] => [13]=> [id_category] => 14 我想得到这个数组的最后一级,即13数组。为了获取这个数组,我使用Category::getN

我从prestashop得到了这个数组:

[11]=>
[id_category] => 11
[children] => 
    [12]=>
        [id_category] => 12
        [children] =>
            [13]=>
                [id_category] => 14
我想得到这个数组的最后一级,即13数组。为了获取这个数组,我使用Category::getNestedCategories

尝试以下操作:

$tree = [
    'id_category' => 11,
    'children' => [
        12 => [
            'id_category' => 12,
            'children' => [
                13 => [
                    'id_category' => 13,
                    'children' => [
                        14 => [
                            'id_category' => 14
                        ]
                    ]
                ],
                15 => [
                    'id_category' => 15
                ]
            ]
        ]
    ]
];

function findMaxDepth(array $data): int
{
    $maxDepth = 0;
    if (isset($data['children'])) {
        // if there'are children of the node collect maxs on this level
        foreach ($data['children'] as $child) {
            // go on next level
            $depth = findMaxDepth($child);
            if ($maxDepth < $depth) {
                $maxDepth = $depth;
            }
        }
    }
    return $maxDepth + 1;
}

结果将是4

尝试以下功能:-

function findLowestChild($arr, $level = 0, $lowestArr = ['level'=>0, 'value'=> null]){

    if(isset($arr['children']) && is_array($arr['children'])) {
        $level++;
        foreach($arr['children'] as $childArr) {
            $lowestArr = findLowestChild($childArr, $level, $lowestArr);
        }
    } else {
        if($lowestArr['level'] < $level) {
            $lowestArr['level'] = $level;
            $lowestArr['value'] = $arr;
        }
    }
    return $lowestArr;
}
函数findLowestChild($arr,$level=0,$lowstar=['level'=>0,'value'=>null]){
if(isset($arr['children'])和&is_数组($arr['children'])){
$level++;
foreach($arr['children']作为$childArr){
$lowstarr=findLowestChild($childArr,$level,$lowstarr);
}
}否则{
如果($Lowstarr['level']<$level){
$Lowstar['level']=$level;
$lowstarr['value']=$arr;
}
}
返回$Lowstar;
}
将其称为findLowestChild($yourray);
它将为您提供级别值以及最低值数组。

的可能副本尽管上面的链接可能会为您提供额外的节点,但这可能是一个有用的起点。如果您有这样的数组,则无论使用何种引擎,解决方案都可能是相同的。但是如果在某个级别上有两个或多个子分支,您希望得到什么??
function findLowestChild($arr, $level = 0, $lowestArr = ['level'=>0, 'value'=> null]){

    if(isset($arr['children']) && is_array($arr['children'])) {
        $level++;
        foreach($arr['children'] as $childArr) {
            $lowestArr = findLowestChild($childArr, $level, $lowestArr);
        }
    } else {
        if($lowestArr['level'] < $level) {
            $lowestArr['level'] = $level;
            $lowestArr['value'] = $arr;
        }
    }
    return $lowestArr;
}