Add value of Childrens nodes to value of parents in array recursively

Asked

Viewed 46 times

0

Good afternoon.

I have the following array structure below, how to sum the values of node_value from children to values node_value of parents, recursively?

Array
    (
    [0] => Array
    (
        [node_id] => 1
        [0] => 1
        [text] => Node A
        [1] => Node A
        [node_value] => 43.5
        [2] => 43.5
        [node_master] => 
        [3] => 
        [children] => Array
        (
            [0] => Array
            (
                [node_id] => 2
                [0] => 2
                [text] => Node B
                [1] => Node B
                [node_value] => 12.4
                [2] => 12.4
                [node_master] => 1
                [3] => 1
                [children] => Array
                (
                    [0] => Array
                    (
                        [node_id] => 5
                        [0] => 5
                        [text] => Node E
                        [1] => Node E
                        [node_value] => 88.9
                        [2] => 88.9
                        [node_master] => 2
                        [3] => 2
                    )
                )
            )
            [1] => Array
            (
                [node_id] => 3
                [0] => 3
                [text] => Node C
                [1] => Node C
                [node_value] => 52.6
                [2] => 52.6
                [node_master] => 1
                [3] => 1
                [children] => Array
                (
                    [0] => Array
                    (
                        [node_id] => 4
                        [0] => 4
                        [text] => Node D
                        [1] => Node D
                        [node_value] => 7.4
                        [2] => 7.4
                        [node_master] => 3
                        [3] => 3
                    )
                    [1] => Array
                    (
                        [node_id] => 8
                        [0] => 8
                        [text] => Node H
                        [1] => Node H
                        [node_value] => 12.8
                        [2] => 12.8
                        [node_master] => 3
                        [3] => 3
                        [children] => Array
                        (
                            [0] => Array
                            (
                                [node_id] => 10
                                [0] => 10
                                [text] => Node J
                                [1] => Node J
                                [node_value] => 90.0
                                [2] => 90.0
                                [node_master] => 8
                                [3] => 8
                                [children] => Array
                                (
                                    [0] => Array
                                    (
                                        [node_id] => 13
                                        [0] => 13
                                        [text] => Node M
                                        [1] => Node M
                                        [node_value] => 18.8
                                        [2] => 18.8
                                        [node_master] => 10
                                        [3] => 10
                                    )
                                )
                            )
                            [1] => Array
                            (
                                [node_id] => 11
                                [0] => 11
                                [text] => Node K
                                [1] => Node K
                                [node_value] => 25.7
                                [2] => 25.7
                                [node_master] => 8
                                [3] => 8
                            )
                            [2] => Array
                            (
                                [node_id] => 15
                                [0] => 15
                                [text] => Node O
                                [1] => Node O
                                [node_value] => 77.0
                                [2] => 77.0
                                [node_master] => 8
                                [3] => 8
                            )
                        )
                    )
                )
            )
            [2] => Array
            (
                [node_id] => 6
                [0] => 6
                [text] => Node F
                [1] => Node F
                [node_value] => 32.1
                [2] => 32.1
                [node_master] => 1
                [3] => 1
                [children] => Array
                (
                    [0] => Array
                    (
                        [node_id] => 7
                        [0] => 7
                        [text] => Node G
                        [1] => Node G
                        [node_value] => 49.7
                        [2] => 49.7
                        [node_master] => 6
                        [3] => 6
                        [children] => Array
                        (
                            [0] => Array
                            (
                                [node_id] => 9
                                [0] => 9
                                [text] => Node I
                                [1] => Node I
                                [node_value] => 19.4
                                [2] => 19.4
                                [node_master] => 7
                                [3] => 7
                                [children] => Array
                                (
                                    [0] => Array
                                    (
                                        [node_id] => 14
                                        [0] => 14
                                        [text] => Node N
                                        [1] => Node N
                                        [node_value] => 2.4
                                        [2] => 2.4
                                        [node_master] => 9
                                        [3] => 9
                                    )
                                )
                            )
                        )
                    )
                    [1] => Array
                    (
                        [node_id] => 12
                        [0] => 12
                        [text] => Node L
                        [1] => Node L
                        [node_value] => 49.7
                        [2] => 49.7
                        [node_master] => 6
                        [3] => 6
                    )
                )
            )
        )
    )
)

I’m trying to get a PHP function to scan the array and add up the values...

1 answer

0

In order for the sum to work in such a vast and nested array it has to be recursive, as already indicated in the question. The detail is that it has to be done from the end to the beginning, so the summing part has to come obligatorily after recursion.

Example of implementation of this logic:

function somaRecursiva(&$arr){

    if (isset($arr["children"])){ 
        //se este nó tem children, a soma tem de ser calculada e por isso começamos com 0
        $arr["node_value"] = 0;

        foreach ($arr["children"] as $key => $value){ //para cada filho no array children
            //somar o resultado desse filho ao value corrente
            $arr["node_value"] += somaRecursiva($arr["children"][$key]);
        }
    }

    return $arr["node_value"] ?? 0; //retornar a soma para cima
}

Example in the ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.