I have a bidimensional array like the follow:
array:4 [
0 => array:3 [
"2504_2512" => 309
"2504_2513" => 654
"2504_2514" => 234
]
1 => array:3 [
"2505_2512" => 143
"2505_2513" => 488
"2505_2514" => 68
]
2 => array:3 [
"2506_2512" => 325
"2506_2513" => 670
"2506_2514" => 250
]
3 => array:3 [
"2507_2512" => 263
"2507_2513" => 608
"2507_2514" => 188
]
]
What I want to do is to sum all values of each column without changing the keys, is it possible to do? Example:
sumColumn1 = 309 + 143 + 325 + 263
sumColumn2 = 654 + 488 + 670 + 608
... I have done this to the first column:
$columnSum = 0;
foreach($array as $row){
$columnSum += $row[array_key_first($row)];
}
But I dont want just the first columns, and I cant see a way to do this without changing the keys.
I already try to search for similar questions, and dont found one, if it exists, I will apreciate if someone let me know.
Edit: Sorry for the incomplete question, I know that I can make another array changing the keys to numeric index using array_values()
. What I want with this question is if it is possible to do it manipulating the original array with the distinct keys (without changing the keys).
And if possible, without using to many for's, as the arrays that I will receive can be much bigger than the one in the question.
The final array will be like:
Array
(
[0] => 1040
[1] => 2420
[2] => 740
)
But I want to Know if it is possible to get there without reindexing the original array.