I'm trying to use array_sum()
on columns within columns of a multidimensional array.
For eg: I have an array that looks like this:
$array = [
[['value' => 1100], ['value' => 1000], ['value' => 3000]],
[['value' => 1200], ['value' => 2000], ['value' => 2000]],
[['value' => 1300], ['value' => 4000], ['value' => 1000]],
];
I tried with:
$arr = [];
foreach($array as $point){
$arr[] = array_sum(array_column($array, $point[0]['value']));
}
print_r($arr);
but I'm expecting this output:
[['value' => 3600], ['value' => 7000], ['value' => 6000]]
Or more simply: [3600, 7000, 6000]
Tested here: https://onecompiler.com/php/3y3mxqky9
You can sum your columns doing like this:
foreach($array as $key => $point){
$arr[] = array_sum(array_column( array_column($array,$key),'value'));
}
print_r($arr);