-1

I have a multidimensional array with nested values. I need to sum values on the lowest level of the nested subarray by keys in the top level. But I am thoroughly stuck.

I have tried various ways of looping thru the layers of the array but have not been able to get a good result yet. I have looked at several SO articles (including this one: php - how to sum a value from 3 dimensional array). My stumbling point is that I have arrays inside arrays on deeper levels and I cannot seem to get the looping right to isolate the values on the deepest level.

Here is an excerpt of the array:

(
    [331] => Array
        (
            [KeyAccountID] => 1234
            [KeyAccountName] => John Lennon
            [ClientID] => 9999
            [Client] => BBC
            [projects] => Array
                (
                    [0] => Array
                        (
                            [0] => 2
                            [1] => 915
                            [2] => Zyxeldy
                            [3] =>  
                            [4] =>  
                            [5] => 15000
                            [6] => 
                            [7] => 
                            [8] =>  
                        )

                    [1] => Array
                        (
                            [0] => 2
                            [1] => 956
                            [2] => Awesome project, Step 1 
                            [3] =>  
                            [4] =>  
                            [5] => 1833.3333
                            [6] => 1833.3333
                            [7] => 1833.3333
                            [8] =>  
                        )

                    [2] => Array
                        (
                            [0] => 2
                            [1] => 957
                            [2] => Awesome project, Step 2 
                            [3] =>  
                            [4] =>  
                            [5] => 7000
                            [6] => 
                            [7] => 
                            [8] =>  
                        )

                )

        )

    [344] => Array
        (
            [KeyAccountID] => 1234
            [KeyAccountName] => John Lennon
            [ClientID] => 9998
            [Client] => ABC
            [projects] => Array
                (
                    [0] => Array
                        (
                            [0] => 2
                            [1] => 487
                            [2] => CRM integration
                            [3] =>  
                            [4] =>  
                            [5] => 
                            [6] => 98750
                            [7] => 98750
                            [8] =>  
                        )

                    [1] => Array
                        (
                            [0] => 2
                            [1] => 839
                            [2] => Data Warehouse
                            [3] =>  
                            [4] =>  
                            [5] => 
                            [6] => 11643.0601
                            [7] => 
                            [8] =>  
                        )

                )

        )

    [350] => Array
        (
            [KeyAccountID] => 1236
            [KeyAccountName] => Ringo Starr
            [ClientID] => 9997
            [Client] => XYY
            [projects] => Array
                (
                    [0] => Array
                        (
                            [0] => 2
                            [1] => 867
                            [2] => Data Mining
                            [3] =>  
                            [4] =>  
                            [5] => 10000
                            [6] => 
                            [7] => 
                            [8] =>  
                        )

                )

        )

    [351] => Array
        (
            [KeyAccountID] => 1235
            [KeyAccountName] => Poul McCartney
            [ClientID] => 9996
            [Client] => XYZ
            [projects] => Array
                (
                    [0] => Array
                        (
                            [0] => 2
                            [1] => 715
                            [2] => XYZ, CSM
                            [3] =>  
                            [4] =>  
                            [5] => 22083.3333
                            [6] => 22083.3333
                            [7] => 22083.3333
                            [8] =>  
                        )

                )

        )
etc.

And here is the code I am working on (for KeyAccountID):

foreach ($arrays as $kam) {
    $kamtotal1[$row['KeyAccountID']] += $row['projects[][5]'];
    $kamtotal2[$row['KeyAccountID']] += $row['projects[][6]'];
    $kamtotal3[$row['KeyAccountID']] += $row['projects[][7]'];
    $kundeansvarlige[$kam['KeyAccountID']][] = $kam;
}

I want to sum the values of the last array column [5] as month1, column [6] as month2, and column [7] as month3 for each KeyAccountID.

Like this:

KeyAccountID,month1,month2,month3
1234,23833.3333,112226.3934,100583.3333
1235, etc.
  • `$row['projects[][5]']` makes no sense. You have no element with the actual key `projects[][5]` anywhere. And what is `$row` even supposed to be? – 04FS Nov 06 '19 at 11:02
  • You’ll want two nested loops here. The inner one loops over the `projects` array. – 04FS Nov 06 '19 at 11:04
  • Thanks @04FS Yeah, the `$row` was a copying mistake, should have been `$kam`. I do understand that I need additional loops but am struggling with how to implement them. Keep getting "Illegal string offset" or getting an array, expecting integer (or words to that effect). – Mads Stenbjerre Nov 06 '19 at 11:40
  • You loop over `projects`, and then access [5], [6] and [7] on that item. – 04FS Nov 06 '19 at 11:59

1 Answers1

0

Use array_sum method to sum of all the values at particular index.

Here is the working example : https://3v4l.org/UEFaB

$newArray = [];
foreach($arrays as $value) {
    $newArray[$value['KeyAccountID']]['month1'] = array_sum(array_column($value['projects'],5));
    $newArray[$value['KeyAccountID']]['month2'] = array_sum(array_column($value['projects'],6));
    $newArray[$value['KeyAccountID']]['month3'] = array_sum(array_column($value['projects'],7));
}
echo "<pre>";
var_dump($newArray);
Omi
  • 3,954
  • 5
  • 21
  • 41
  • Thanks @Omi I got it to work. Would you happen to be able to tell me how to filter the results by column [0] in the final array? It can take the values 1 and 2, and I would like the month1, month2, and month3 totals for 1 and 2 separately. – Mads Stenbjerre Nov 06 '19 at 13:39
  • you can easily access. Now I used keyAccountId as key for array which will have 3 keys again like month1,month2,month3. Simply iterate array then for months 1 use key month1 like `$value['month1']` – Omi Nov 07 '19 at 10:29