On a problem of PHP Recursive processing Statistics

json data is:

{
    "code": 0,
    "msg": "",
    "data": {
        "2": {
            "project_id": 2,
            "project_name": "V",
            "project_parent_id": 0,
            "_completed": 0,
            "_uncompleted": 2,
            "_count": 2,
            "children": [
                {
                    "project_id": 6,
                    "project_name": "",
                    "project_parent_id": 2,
                    "_completed": 0,
                    "_uncompleted": 0,
                    "_count": 0
                },
                {
                    "project_id": 10,
                    "project_name": "",
                    "project_parent_id": 2,
                    "_completed": 0,
                    "_uncompleted": 0,
                    "_count": 0,
                    "children": [
                        {
                            "project_id": 11,
                            "project_name": "",
                            "project_parent_id": 10,
                            "_completed": 0,
                            "_uncompleted": 0,
                            "_count": 0
                        }
                    ]
                }
            ]
        }
    },
    "debug": {
        "sql": "SELECT `project_id`,`project_name`,`project_parent_id` FROM  `odp_project` WHERE `project_id` = "2" OR `root` = "2" LIMIT 0,4",
        "entry": []
    }
}

the requirement is to count the _ completed,_uncompleted,_count in the lower and middle levels of the data to the upper level. Please tell me how to write this recursion.

Php
Mar.21,2021

cycle out the superior first, then use array_column to extract the three fields of the subordinate, and then array_sum to sum them, which is the data you want.

Menu