0

首先,对不起我的英语。

我想通过任何级别的数组,但我想从底层到上一层并递归更新键的值,但是一个例子比一个文本更好:

这是我的示例代码:

Array
(
    [1] => Array
        (
            [ItemText] => Home
            [ItemLink] => index.php
            [count] => 0
            [id] => 1
            [ParentID] => 
            [Children] => Array
                (
                    [2] => Array
                        (
                            [ItemText] => Home Sub 1
                            [ItemLink] => somepage.php
                            [id] => 2
                            [count] => 0
                            [ParentID] => 1
                            [Children] => Array
                                (
                                    [3] => Array
                                        (
                                            [ItemText] => Home Sub 2
                                            [ItemLink] => somepage2.php
                                            [id] => 3
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [ItemText] => Contact
                                            [ItemLink] => contact.php
                                            [id] => 4
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

请注意任何级别数组中的计数键。每个级别都是“当前”位置的子级。我需要这个:

Array
(
    [1] => Array
        (
            [ItemText] => Home
            [ItemLink] => index.php
            [count] => **2**
            [id] => 1
            [ParentID] => 
            [Children] => Array
                (
                    [2] => Array
                        (
                            [ItemText] => Home Sub 1
                            [ItemLink] => somepage.php
                            [id] => 2
                            [count] => **2**
                            [ParentID] => 1
                            [Children] => Array
                                (
                                    [3] => Array
                                        (
                                            [ItemText] => Home Sub 2
                                            [ItemLink] => somepage2.php
                                            [id] => 3
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [ItemText] => Contact
                                            [ItemLink] => contact.php
                                            [id] => 4
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

我想累积和总结当前位置的所有孩子的计数,然后通过上一个级别并再次累积当前级别的所有计数,这就是上一级。

我感谢您的所有帮助。提前致谢。

编辑

我根据我的需要调整了@HamzaKubba 的功能,这对我有用。我把这个给那些需要的人:

function explore(& $node) {
    $count = 0;
    if (count($node) > 0) {
        foreach ($node as &$value) {
            if (!isset($value['count']))
                $value['count'] = 0;

            if (count($value['Children']) > 0)
                $value['count'] += explore($value['Children']);

            $count += $value['count'];
        }
    }
    return $count;
}
4

1 回答 1

2

通用模板的伪代码:

function explore(node) {
    foreach (child of node) {
         explore(child)
    }
    // now that we're done with children, do logic/calculation here
    doSomething(node)
}

你想要的伪代码(据我了解):

function explore(node) {
    foreach (child of node) {
         node.count = node.count + explore(child)
    }
    return node.count
}

更正您的新代码:

function explore_($node) {
    if (!isset($node['count']))
        $node['count'] = 0;

    foreach ($node['Children'] as $child) {
        $node['count'] = $node['count'] + explore_($child);
    }

    return $node['count'];
}
于 2013-10-15T03:19:35.023 回答