Add value of childrens nodes to parents value in array recursively

0

Good afternoon.

I have the following array structure below, how to sum the values of node_value of child nodes to node_value values 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 the values ...

    
asked by anonymous 22.10.2017 / 17:31

1 answer

0

For the sum to work in such a large and nested array it will have 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 part that sums must come after the recursion.

Example of implementing 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 on ideone

    
22.10.2017 / 19:18