0

PHP recursive function to sum specifix value in all childs

I was facing an issue to sum all child values in an array and I just created a simple function to solve my issue. I'm sharing this for knowledge improvement.

$json = '{
    "name": "ABC",
    "subArr": [{
            "name": "def",
            "count": 10,
            "subArr": [{
                    "name": "ghi",
                    "count": 5
                }, {
                    "name": "jkl",
                    "count": 15,
                    "subArr": [{
                            "name": "mno",
                            "count": 5
                        }]
                }]
        }, {
            "name": "pqr",
            "count": 10,
            "subArr": [{
                    "name": "stu",
                    "count": 5
                }, {
                    "name": "vwx",
                    "count": 15,
                    "subArr": [{
                            "name": "yz",
                            "count": 5,
                            "subArr": [{
                                    "name": "yz",
                                    "count": 5,
                                    "subArr": [{
                                            "name": "yz",
                                            "count": 5
                                        }]
                                }]
                        }]
                }]
        }]
}';

1 Answer 1

1
$json = '{
    "name": "ABC",
    "subArr": [{
            "name": "def",
            "count": 10,
            "subArr": [{
                    "name": "ghi",
                    "count": 5
                }, {
                    "name": "jkl",
                    "count": 15,
                    "subArr": [{
                            "name": "mno",
                            "count": 5
                        }]
                }]
        }, {
            "name": "pqr",
            "count": 10,
            "subArr": [{
                    "name": "stu",
                    "count": 5
                }, {
                    "name": "vwx",
                    "count": 15,
                    "subArr": [{
                            "name": "yz",
                            "count": 5,
                            "subArr": [{
                                    "name": "yz",
                                    "count": 5,
                                    "subArr": [{
                                            "name": "yz",
                                            "count": 5
                                        }]
                                }]
                        }]
                }]
        }]
}';


function countTotal($array, &$total) {
    foreach ($array["subArr"] as $row) {
        $total = $total + $row['count'];
        if (!empty($row["subArr"])) {
            countTotal($row, $total);
        }
    }
    return $total;
}

$total = 0;

$finalCount = countTotal(json_decode($json, true), $total);
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.