I have a multi-level object data with a structure similar to the window below. 

I am trying to

* sum all the direct package values for the top users or say `parents`, only
* sum all the nested indirect package values from the `children` tree.

The final result is expected to be similar to:
`[{"sum_direct": 600},{"sum_indirect": 3000}]`

I need help to know what I'm not doing right:

```
function sumDirect($c) {
    if(count($c) > 0){
        $sum = 0;
        foreach ($c as $package) {
            $sum += $package['package']['direct'];
        }
    } else {
        $sum = 0;
    }
    return $sum;
}

function sumIndirect($c) {
    if(count($c) > 0){
        $sum = 0;
        foreach ($c as $package) {
            if(count($package['children']) > 0) {
                foreach ($package['children'] as $children){
                    $sum += $children['indirect'];
                    $sum += sumIndirect($children);
                }
            }
        }
    } else {
        $sum = 0;
    }
    return $sum;
}

```

Sample input:

```
{
  "users": [{
      "user_id": 2,
      "ref_id": 1,
      "package": [{
          "name": "Basic"
        },
        {
          "direct": 200
        },
        {
          "indirect": 100
        }
      ],
      "children": [{
          "user_id": 58,
          "ref_id": 2,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 59,
          "ref_id": 2,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 111,
          "ref_id": 2,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 116,
          "ref_id": 2,
          "package": [{
              "name": "Diamond"
            },
            {
              "direct": 1000
            },
            {
              "indirect": 500
            }
          ],
          "children": 0
        },
        {
          "user_id": 119,
          "ref_id": 2,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        }
      ]
    },
    {
      "user_id": 100,
      "ref_id": 1,
      "package": [{
          "name": "Basic"
        },
        {
          "direct": 200
        },
        {
          "indirect": 100
        }
      ],
      "children": [{
          "user_id": 101,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 102,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 103,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 104,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 105,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 106,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 107,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 108,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 109,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 110,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        },
        {
          "user_id": 117,
          "ref_id": 100,
          "package": [{
              "name": "Diamond"
            },
            {
              "direct": 1000
            },
            {
              "indirect": 500
            }
          ],
          "children": 0
        },
        {
          "user_id": 129,
          "ref_id": 100,
          "package": [{
              "name": "Diamond"
            },
            {
              "direct": 1000
            },
            {
              "indirect": 500
            }
          ],
          "children": 0
        },
        {
          "user_id": 130,
          "ref_id": 100,
          "package": [{
              "name": "Basic"
            },
            {
              "direct": 200
            },
            {
              "indirect": 100
            }
          ],
          "children": 0
        }
      ]
    }
  ]
}

```

Complete data at [users.json](https://github.com/bishoplee/mlu/edit/master/users.json)