Need a little help here with summing up arrays inside a multi dimensional php array
Eg Multidimensional array
Array
(
[0] => Array
(
This should work for you:
$v)
$result[$k] = array_sum(array_column($arr, $k));
print_r($result);
?>
Output:
Array ( [0] => 80 [1] => 9 [2] => 14 [3] => 11 [4] => 12 [5] => 15 [6] => 11 [7] => 7 )
EDIT:
If your php version is under 5.4 you can use this simple workaround:
function arrayColumn(array $array, $column_key, $index_key=null){
if(function_exists('array_column ')){
return array_column($array, $column_key, $index_key);
}
$result = [];
foreach($array as $arr){
if(!is_array($arr)) continue;
if(is_null($column_key)){
$value = $arr;
}else{
$value = $arr[$column_key];
}
if(!is_null($index_key)){
$key = $arr[$index_key];
$result[$key] = $value;
}else{
$result[] = $value;
}
}
return $result;
}
(Note that you have to use arrayColumn()
)
From the manual comments: http://php.net/manual/de/function.array-column.php#116301