doulangdang9986 2015-07-21 07:03
浏览 38
已采纳

嵌套数组中子项的递归计数+通过引用更改值

I know this is basic recursion but I get stuck anyway :(

I need to count how many elements each element has below it (children, grandchildren,...) and write that value into original array.

My example array:

$table = [
1 => [
    'id' => 1,
    'children_count' => 0
],
2 => [
    'id' => 2,
    'children_count' => 0,
    'children' => [
        3 => [
            'id' => 3,
            'children_count' => 0,
            'children' => [
                4 => [
                    'id' => 4,
                    'children_count' => 0,
                    'children' => [
                        5 => [
                            'id' => 5,
                            'children_count' => 0
                        ],

                        6 => [
                            'id' => 6,
                            'children_count' => 0
                        ]
                    ]
                ]
            ]
        ]
    ]
]    
];

My recursion code:

function count_children(&$array){
    foreach($array['children'] as &$child){
        if(isset($child['children'])){
            $array['children_count'] += count_children($child);
        }
        else return 1;
    }
}

Call for recursion:

//call for each root element
foreach($table as &$element){
    if(isset($element['children'])) count_children($element);
}

Expected output:

$table = [
1 => [
'id' => 1,
'children_count' => 0
 ],
 2 => [
'id' => 2,
'children_count' => 4,
'children' => [
    3 => [
        'id' => 3,
        'children_count' => 3,
        'children' => [
            4 => [
                'id' => 4,
                'children_count' => 2,
                'children' => [
                    5 => [
                        'id' => 5,
                        'children_count' => 0
                    ],

                    6 => [
                        'id' => 6,
                        'children_count' => 0
                    ]
                ]
            ]
        ]
    ]
]
]    
];

Where did I got it wrong? My function does something, element 3 gets value 1, but thats about it.

Here is the ideone link: http://ideone.com/LOnl3G

  • 写回答

1条回答 默认 最新

  • douguxun6866 2015-07-21 07:31
    关注
    function count_children(&$table){
      $count1 = 0;
      foreach($table as &$array) {
        $count = 0;
        if (isset($array['children'])) {
          $count += count($array['children']);
          $count += count_children($array['children']);
        }
        $array['children_count'] = $count;
        $count1 += $count;
      }
      return $count1; 
    }
    
    count_children($table);
    
    print_r($table);   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退