Umer Javed
Umer Javed

Reputation: 31

PHP recursive function to sum specifix value in all childs

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

Upvotes: 0

Views: 160

Answers (1)

Umer Javed
Umer Javed

Reputation: 31

$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);

Upvotes: 1

Related Questions