Anibal Mauricio
Anibal Mauricio

Reputation: 553

go array from bottom to up level

First of all, sorry for my english.

I want to go through an array of any levels, but I want to go in the bottom level to up level and update a value of key recursively, but an example is better than a text:

this is my example code:

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
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

note the count key in any level of array. Each level is a child of the "current" position. I need this:

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
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

I want to accumulate and sum the count of all child in the current position and go through the prev level and accumulate again all count of the current level and that's to the up level.

I appreciate all your help. Thanks in advance.

EDIT

I adapted the function of @HamzaKubba to my needs and this works for me. I put this for those to require:

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;
}

Upvotes: 0

Views: 677

Answers (1)

Hamza Kubba
Hamza Kubba

Reputation: 2269

pseudoCode of general template:

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

pseudoCode of what you want (from what I understand):

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

to correct your new code:

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

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

    return $node['count'];
}

Upvotes: 2

Related Questions