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.