-1

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Joaonic
  • 191
  • 9
  • https://stackoverflow.com/questions/12706359/how-to-group-subarrays-by-a-column-value might be your friend – Jim Panse Jan 05 '21 at 14:33
  • 1
    What do you mean by "not changing the keys"? Which keys are you looking to retain? – El_Vanja Jan 05 '21 at 14:49
  • @JimPanse the keys of the subarrays in this question are the same, as you can see, in my case, the keys of the values in the subarrays are distinct. – Joaonic Jan 05 '21 at 15:07
  • @El_Vanja I mean the keys for the values in the subarrays. – Joaonic Jan 05 '21 at 15:08
  • 1
    I still fail to understand how you wish to preserve them (as each is different) and at the same time have the sum... can you post what you'd like the final array with sums to look like (keys and values)? – El_Vanja Jan 05 '21 at 15:11
  • @El_Vanja sorry for the misconception, I have edited the question, is it better now? – Joaonic Jan 05 '21 at 15:22
  • add result you wanna get – V-K Jan 05 '21 at 15:27
  • What do you mean by `What I want with this question is if it is possible to do it with the array with the distinct keys`? If these are all distinct the result would only be an array with the keys 1 level up? Since the sum would be each individual value for now? Or which of those string keys would you find fitting to index the result with? – Remy Jan 05 '21 at 15:33
  • @Remy Sorry, english is not my native language, so it is kinda difficult to explain thing in english. I think that is exactly that I wanna know, by your answer I presume that I cant sum the columns with the distinct keys? As there are no standards, I cant use this array without changing the indexes ? (when I say changing the indexes, I mean like the answer given by @V-K) – Joaonic Jan 05 '21 at 15:44
  • @Joaonic it depends, technically you could repeat the respective column sum with every key. Really depends on how you envision your output to look like. (Although it is hard to see the added value of this) – Remy Jan 05 '21 at 15:50
  • @Remy the output doesnt really matter, as what I want to know is a way to do it without changing the keys of the values from the original subarrays, I mean, without doing array_values() in each subarray to do the sum, and as I say, if it is possible using few loops. – Joaonic Jan 05 '21 at 15:52

1 Answers1

1
<?php
$array = [
  0 => [
    "2504_2512" => 309,
    "2504_2513" => 654,
    "2504_2514" => 234,
  ],
  1 => [
    "2505_2512" => 143,
    "2505_2513" => 488,
    "2505_2514" => 68,
  ],
  2 => [
    "2506_2512" => 325,
    "2506_2513" => 670,
    "2506_2514" => 250
  ],
  3 => [
    "2507_2512" => 263,
    "2507_2513" => 608,
    "2507_2514" => 188
  ]
];
$columnSums = [];    
foreach($array as $data){
    $values = array_values($data);
    foreach ($values as $key => $value) {
         if (empty($columnSums[$key])) {
             $columnSums[$key] = $value;
         } else {
             $columnSums[$key] += $value;
         } 
     }
}

print_r($columnSums);

result:

Array
(
    [0] => 1040
    [1] => 2420
    [2] => 740
)

Just use array_values to get a new array with values and keys like 0,1,2

V-K
  • 1,297
  • 10
  • 28
  • I believe the goal is to retain the associative keys (`2504_2512` etc.). – El_Vanja Jan 05 '21 at 14:43
  • As I understood, the post owner wants to get the sum of the array values. That code does it – V-K Jan 05 '21 at 14:45
  • Sorry, I might have misunderstood the question, since it seemed to me that the keys on the second level are the same. I see now that they are not. – El_Vanja Jan 05 '21 at 14:48
  • @V-K thanks for the answer, I will accept this answer if it is not possible to do the way I want to. – Joaonic Jan 05 '21 at 15:28
  • 1
    @Joaonic what is "the way I want to"? How is the result different from what you want? – miken32 Jan 06 '21 at 02:44
  • @miken32 what I have wanted with this question is not the result, but the way to do it, I wanted to know if it was possible to do it in the way I have specified in the question. But I think that using array_values() to reindex every subarray is the only way to do it. – Joaonic Jan 06 '21 at 13:28