算上嵌套数组树叶的数量 [英] Count number of leaves in nested array tree

查看:153
本文介绍了算上嵌套数组树叶的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套数组的树,从下面的函数平板阵列产生:

 函数convertToTree(数组$持平,$ idField ='ID',
                        $ parentIdField ='parentId的',
                        $ childNodesField ='子节点'){
    $指数=阵列();
    //第一关 - 取得由主ID索引的数组
    的foreach($平如$行){
        $索引[$行[$ idField] = $行;
        $索引[$行[$ idField] [$ childNodesField] =阵列();
    }    //第二遍
    $根= NULL;
    的foreach($建立索引的$ id => $行){
        $索引[$行[$ parentIdField] [$ childNodesField] [$ ID] =安培; $索引[$ ID];
        如果(!$行[$ parentIdField]){
            $根= $ ID;
        }    }
    返回数组($根=> $索引[$根]);
}

我需要添加一个条目叶数我的阵列中的每个节点。此条目应算节点的所有子节点的所有叶子:

 阵列(
    [9] =>阵列(
        [ID] => 9,
        [parentId的] =>空值,
        [名] =>项目0,
        [叶片数=> 4,(对应于叶片100和101 + 200和201)
        [子节点] =>排列
            (
                [1] =>阵列(
                    [ID] => 1,
                    [parentId的] => 9,
                    [名] =>项目1,
                    [叶片数=> 2,(对应于叶片100和101)
                    [子节点] =>阵列(
                        [10] =>阵列(
                            [ID] => 10,
                            [parentId的] => 1,
                            [名] =>第10项,
                            [子节点] =>阵列(
                                [100] =>阵列(
                                    [ID] => 100,
                                    [parentId的] => 10,
                                    [名] => 100项,
                                    [子节点] =>阵列()
                                )
                                [101] =>阵列(
                                    [ID] => 101,
                                    [parentId的] => 10,
                                    [名] =>项目101,
                                    [子节点] =>阵列()
                                )
                            )
                        )
                    )
                )
                [2] =>阵列(
                    [ID] => 2,
                    [parentId的] => 9,
                    [名] =>第2项,
                    [叶片数=> 2,(对应于叶片200和201)
                    [子节点] =>阵列(
                        [20] =>阵列(
                            [ID] => 20,
                            [parentId的] => 2,
                            [名] =>项目20,
                            [子节点] =>阵列(
                                [200] =>阵列(
                                    [ID] => 200,
                                    [parentId的] => 20,
                                    [名] => 200项,
                                    [子节点] =>阵列()
                                )
                                [201] =>阵列(
                                    [ID] => 201,
                                    [parentId的] => 20,
                                    [名] =>项201,
                                    [子节点] =>阵列()
                                )
                            )
                        )
                    )
                )
            )
    )


解决方案

这可以解决你的叶数的问题,我的阵列中的每个节点。此条目应算节点

的所有子节点的所有的叶子

其从PHP 手动
        `$ =食品阵列('水果'=>阵列('橙色','香蕉','苹果'),
              '素食'=>阵列(胡萝卜,羽衣,豌豆'));

  //递归数
     回声计数($食品,COUNT_RECURSIVE); //输出8     //正常计数
     回声计数($食物); //输出2`

I have a nested array tree, generated from a flat array with the following function :

function convertToTree(array $flat, $idField = 'id',
                        $parentIdField = 'parentId',
                        $childNodesField = 'childNodes') {
    $indexed = array();
    // first pass - get the array indexed by the primary id  
    foreach ($flat as $row) {
        $indexed[$row[$idField]] = $row;
        $indexed[$row[$idField]][$childNodesField] = array();
    }

    //second pass  
    $root = null;
    foreach ($indexed as $id => $row) {
        $indexed[$row[$parentIdField]][$childNodesField][$id] =& $indexed[$id];
        if (!$row[$parentIdField]) {
            $root = $id;
        }

    }
    return array($root => $indexed[$root]);
}

I would need to add an entry "NUMBER OF LEAVES" for each node of my array. This entry should count ALL the leaves of all the subnodes of the node :

Array ( 
    [9] => Array ( 
        [id] => 9, 
        [parentId] => null,
        [name] => Item 0, 
        [NUMBER OF LEAVES] => 4,  (corresponding to leaves 100 and 101 + 200 and 201)
        [childNodes] => Array 
            ( 
                [1] => Array ( 
                    [id] => 1, 
                    [parentId] => 9, 
                    [name] => Item 1, 
                    [NUMBER OF LEAVES] => 2,   (corresponding to leaves 100 and 101)
                    [childNodes] => Array ( 
                        [10] => Array ( 
                            [id] => 10, 
                            [parentId] => 1, 
                            [name] => Item 10, 
                            [childNodes] => Array ( 
                                [100] => Array ( 
                                    [id] => 100, 
                                    [parentId] => 10, 
                                    [name] => Item 100, 
                                    [childNodes] => Array ( ) 
                                ) 
                                [101] => Array ( 
                                    [id] => 101, 
                                    [parentId] => 10, 
                                    [name] => Item 101, 
                                    [childNodes] => Array ( ) 
                                ) 
                            ) 
                        ) 
                    ) 
                ) 
                [2] => Array ( 
                    [id] => 2, 
                    [parentId] => 9, 
                    [name] => Item 2, 
                    [NUMBER OF LEAVES] => 2,   (corresponding to leaves 200 and 201)
                    [childNodes] => Array ( 
                        [20] => Array ( 
                            [id] => 20, 
                            [parentId] => 2, 
                            [name] => Item 20, 
                            [childNodes] => Array ( 
                                [200] => Array ( 
                                    [id] => 200, 
                                    [parentId] => 20, 
                                    [name] => Item 200, 
                                    [childNodes] => Array ( ) 
                                ) 
                                [201] => Array ( 
                                    [id] => 201, 
                                    [parentId] => 20, 
                                    [name] => Item 201, 
                                    [childNodes] => Array ( ) 
                                ) 
                            ) 
                        ) 
                    ) 
                ) 
            ) 
    ) 
)

解决方案

this could solve your problem of "NUMBER OF LEAVES" for each node of my array. This entry should count ALL the leaves of all the subnodes of the node"

its from php manual `$food = array('fruits' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea'));

     // recursive count
     echo count($food, COUNT_RECURSIVE); // output 8

     // normal count
     echo count($food); // output 2`

这篇关于算上嵌套数组树叶的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆