PHP Tricks
September 11, 2022

How to get sum of one multidimensional array column?

<?php
$data = [
    ['id' => '3202','total' => '5'],
    ['id' => '3190','total' => '2'],
    ['id' => '3199','total' => '5']
];

$total_column = array_column($data, 'total');

$sum = array_sum($total_column);

printf('The sum of total column: %s', $sum);

php code online