Issue with looping and iterating/adding

I have a data array right now, and it produces a format that is mostly the same but in every level I have a totals index that is slightly different from the items before it.

I’m currently looping it but having an issue with each iteration’s “total” index because it has a flatter format and not as many levels. I’m using “Adam” as the example here but the idea is that there are multiple people with this same exact formatted array, and I want to total by category, then total by department( “01” and “04”, and then a grand total for “Adam”, “Sam”, etc.

What am I doing wrong?

Code:

"Adam" => array (
"01" => array (
  "categories" => array(
    "CategoryOne"=> array(
      "downloads" => 10,
      "saves" => 2,
      "expected"=> array(
        "possible" => 30,
      ),
    ),
    "CategoryTwo"=> array(
      "downloads" => 15,
      "saves" => 8,
      "expected"=> array(
        "possible" => 15,
      ),
    ),
    "total"=> array(     //this totals all categories
      "downloads" => 25,
      "saves" => 10,
      "expected"=> array(
        "possible" => 45,
      ),
    ),
  ),
  "total"=> array(     //this would total anything under "01" like "categories", "items", etc.
    "downloads" => 25,
    "saves" => 10,
    "expected"=> array(
      "possible" => 45,
    ),
  ) ,     
),
"04" => array (
  "categories" => array(
    "CategoryOne"=> array(
      "downloads" => 10,
      "saves" => 2,
      "expected"=> array(
        "possible" => 30,
      ),
    ),
    "CategoryTwo"=> array(
      "downloads" => 15,
      "saves" => 8,
      "expected"=> array(
        "possible" => 15,
      ),
    ),
    "total"=> array(      //totals categories
      "downloads" => 25,
      "saves" => 10,
      "expected"=> array(
        "possible" => 45,
      ),
    ),
  ),
  "total"=> array(         //this would total anything under "04" like "categories", "items", etc.
    "downloads" => 25,
    "saves" => 10,
    "expected"=> array(
      "possible" => 45,
    ),
  ),        
),
"total"=> array(         //this would total "01" and "04".
  "downloads" => 50,
  "saves" => 20,
  "expected"=> array(
    "possible" => 90,
  ),
),
),
"total"=> array(         //this would be a grand total for Adam as well as any other people in the array
  "downloads" => 50,
  "saves" => 20,
  "expected"=> array(
    "possible" => 90,
  ),
);

$results = [];


foreach ($data as $i) {
    foreach ($i as $group => $n) {
        if (!array_key_exists($group, $results)) $results[$group] = [];
        foreach ($n as $category_name => $category) {
            if (!array_key_exists($category_name, $results[$group])) $results[$group][$category_name] = [];
            foreach ($category as $item_name => $item) {
                if (!array_key_exists($item_name, $results[$group][$category_name])) {
                    $results[$group][$category_name][$item_name] = $item;
                } else {
                    foreach ($item as $purpose => $dollars) {
                        $results[$group][$category_name][$item_name][$purpose] += $dollars;
                    }
                }
            }
        }
    }
};

dd($results);

Desired output would be similar but would total for all users, not just Adam:

"01" => array (
  "categories" => array(
    "CategoryOne"=> array(
      "downloads" => 10,
      "saves" => 2,
      "expected"=> array(
        "possible" => 30,
      ),
    ),
    "CategoryTwo"=> array(
      "downloads" => 15,
      "saves" => 8,
      "expected"=> array(
        "possible" => 15,
      ),
    ),
    "total"=> array(     //this totals all categories
      "downloads" => 25,
      "saves" => 10,
      "expected"=> array(
        "possible" => 45,
      ),
    ),
  ),
  "total"=> array(     //this would total anything under "01" like "categories", "items", etc.
    "downloads" => 25,
    "saves" => 10,
    "expected"=> array(
      "possible" => 45,
    ),
  ) ,     
),
"04" => array (
  "categories" => array(
    "CategoryOne"=> array(
      "downloads" => 10,
      "saves" => 2,
      "expected"=> array(
        "possible" => 30,
      ),
    ),
    "CategoryTwo"=> array(
      "downloads" => 15,
      "saves" => 8,
      "expected"=> array(
        "possible" => 15,
      ),
    ),
    "total"=> array(      //totals categories
      "downloads" => 25,
      "saves" => 10,
      "expected"=> array(
        "possible" => 45,
      ),
    ),
  ),
  "total"=> array(         //this would total anything under "04" like "categories", "items", etc.
    "downloads" => 25,
    "saves" => 10,
    "expected"=> array(
      "possible" => 45,
    ),
  ),        
),
"total"=> array(         //this would total "01" and "04".
  "downloads" => 50,
  "saves" => 20,
  "expected"=> array(
    "possible" => 90,
  ),
),
),
"total"=> array(         //this would be a grand total for Adam as well as any other people in the array
  "downloads" => 50,
  "saves" => 20,
  "expected"=> array(
    "possible" => 90,
  ),

Do you mean that you wish to know how to query the database to get this results or are you asking to handle this inside arrays?

Normally, you would store this data in a database and run the correct queries to produce your totals.
Is your array created from a query or manually somehow?

As ErnieAlex mentioned, you need this in a database. Otherwise performance is going to be a large issue with even just a hundred people. Take the time to learn a database. It’s simply so much easier and quicker. You’ll wonder, and kick yourself, why you didn’t learn it earlier.

Sponsor our Newsletter | Privacy Policy | Terms of Service