I have this this array in my code
Array
(
[1] => Array
(
[1] => 4
[5] => 7
)
[2] => Array
(
[1] => 2
[5] => 5
)
[3] => Array
(
[1] => 2
[5] => 5
)
[4] => Array
(
[1] => 6
[5] => 9
)
[5] => Array
(
[1] => 6
[5] => 8
)
)
1
How could I get sum element with same index? Forexample - sum of all element with index 5 or index 1. If it possible without hardcode.
You could do the following:
This way you'll loop through all your arrays, and get the complete sum of a specific ID.
You can use it like this
getSumOfId($yourArray, 5)
You can use
array_reduce()
to sum target values.Check code result in demo
you can use this code
this is demo code for test