从无限多维数组级别计算

时间:2018-02-05 14:19:58

标签: php arrays multidimensional-array

任何有智慧的人都可以帮助我制作计算阵列中无限级别孩子的功能吗?

如果你看到我在父母“A”的孩子的孩子中有孩子的3个等级,在下面

。到目前为止,我正在使用php for-loop遍历每个级别的数组并进行一些计算,这些计算应该可以从下面的函数example_TreeCalc中理解。基本上我会进入所有级别的数组,并在两个不同的变量中总结revPointsrevPoints_calculated。后来我减去它们。

PS:我的朋友告诉我DFS解决这个问题。我检查了那个算法,但找不到通过DFS进行这些计算的方法。如果有人愿意使用DFS创建任何通过给定数组进行以下计算的示例。我也将非常感谢。

谢谢!

$example_Tree = array(
    'name'      => 'A',
    'revPoints' => '300.00',
    'revPoints_calculated' => '120.00',
    'child'=>  array(
        array(
            'name' => 'B',
            'revPoints' => '250.00',
            'revPoints_calculated' => '30.00',
            'child' => array(
                array(
                    'name' => 'C',
                    'revPoints' => '0.00',
                    'revPoints_calculated' => '0.00',
                    'child' => array()

                ),
                array(
                    'name' => 'D',
                    'revPoints' => '250.00',
                    'revPoints_calculated' => '10.00',
                    'child' => array()

                ),
                array(
                    'name' => 'E',
                    'revPoints' => '1150.00',
                    'revPoints_calculated' => '92.00',
                    'child' => array()
                ),
            )
        ),
        array(
            'name' => 'C',
            'revPoints' => '1200.00',
            'revPoints_calculated' => '541.00',
            'child' => array(
                array(
                    'name' => 'D',
                    'revPoints' => '2000.00',
                    'revPoints_calculated' => '160.00',
                    'child' => array()

                ),
                array(
                    'name' => 'E',
                    'revPoints' => '3500.00',
                    'revPoints_calculated' => '420.00',
                    'child' => array()

                ),
                array(
                    'name' => 'F',
                    'revPoints' => '850.00',
                    'revPoints_calculated' => '34.00',   
                    'child' => array()

                ),
                array(
                    'name' => 'G',
                    'revPoints' => '150.00',
                    'revPoints_calculated' => '0.00',
                    'child' => array()
                ),
            )
        ),
        array(
            'name' => 'D',
            'revPoints' => '800.00',
            'revPoints_calculated' => '100.00',
            'child' => array(
                array(
                    'name' => 'E',
                    'revPoints' => '300.00',
                    'revPoints_calculated' => '12.00',
                    'child' => array()

                ),
                array(
                    'name' => 'F',
                    'revPoints' => '300.00',
                    'revPoints_calculated' => '12.00',
                    'child' => array()

                ),
                array(
                    'name' => 'G',
                    'revPoints' => '150.00',
                    'revPoints_calculated' => '0.00',
                    'child' => array()
                ),
            )

        ),
        array(
            'name' => 'E',
            'revPoints' => '100.00',
            'revPoints_calculated' => '0.00',
            'child' => array(
                array(
                    'name' => 'F',
                    'revPoints' => '800.00',
                    'revPoints_calculated' => '32.00',
                    'child' => array()
                ),
                array(
                    'name' => 'G',
                    'revPoints' => '350.00',
                    'revPoints_calculated' => '76.00',
                    'child' => array(
                        array(
                            'name' => 'H',
                            'revPoints' => '800.00',
                            'revPoints_calculated' => '32.00',
                        ),
                        array(
                            'name' => 'I',
                            'revPoints' => '100.00',
                            'revPoints_calculated' => '0.00',
                        ),
                    )
                ),
            )
        ),
    ),
);

dump($example_Tree);


function example_TreeCalc($tree) {
    $treeParent = $tree['name'];
    $revPercentTotal =  0;
    $revTotal =  0;
    $childTree_level_1 = $tree['child'];
    // dump('child Tree level 1=>', count($childTree_level_1));
    if (isset($childTree_level_1)) {
        for ($i=0; $i < count($childTree_level_1); $i++) { 

            $childTree_level_2 = $childTree_level_1[$i]['child'];
            // dump('child Tree level 2=>', count($childTree_level_2));
            if (isset($childTree_level_2)) {

                for ($j=0; $j < count($childTree_level_2); $j++) {

                    $childTree_level_3 = $childTree_level_2[$j]['child'];
                    // dump('child Tree level 3=>', count($childTree_level_3));
                    if (isset($childTree_level_3)) {

                        for ($k=0; $k < count($childTree_level_3); $k++) {
                            $revPercentTotal += $childTree_level_3[$k]['revPoints_calculated'];
                            $revTotal += $childTree_level_3[$k]['revPoints'];
                        }

                    }
                    $revPercentTotal += $childTree_level_2[$j]['revPoints_calculated'];
                    $revTotal += $childTree_level_2[$j]['revPoints'];                
                }

            }
            $revPercentTotal += $childTree_level_1[$i]['revPoints_calculated'];
            $revTotal += $childTree_level_1[$i]['revPoints'];
        }
    }

    echo "BEFORE revPercentTotal $revPercentTotal" . "<br/>";
    echo "BEFORE revTotal $revTotal" . "<br/>";

    // $revPercentTotal += $tree['revPoints_calculated'];
    // $revTotal += $tree['revPoints'];


    echo "revTotal $revTotal " . "<br/>";
    echo "revPercentTotal $revPercentTotal" . "<br/>";


    $commission = number_format($revTotal - $revPercentTotal,2);

    return array('Name'=> $treeParent, 'Commission'=> $commission); 
}

dump(exampleTreeCalc($example_Tree));

1 个答案:

答案 0 :(得分:1)

如果不涉及除简单求和之外的其他逻辑,则可以使用array_walk_recursive

$revPercentTotal = 0;
$revTotal = 0;
array_walk_recursive($example_Tree, function ($value, $key) use (&$revPercentTotal, &$revTotal) {
    if ($key === 'revPoints_calculated') {
        $revPercentTotal += $value;
    } elseif ($key === 'revPoints') {
        $revTotal += $value;
    }
});

$commision = number_format($revTotal - $revPercentTotal, 2);

这是the demo