How to Sum Values of an Array With the Same Key in PHP?

Let's suppose you have an array of items like the following:

$items = [
    'item_1' => ['id' => 1, 'name' => 'Pink Shirt', 'qty' => 2],
    'item_2' => ['id' => 2, 'name' => 'Blue Shirt', 'qty' => 3],
];

To sum the values of a specific key (such as qty) in the array, you can use any of the following:

  1. foreach Loop;
  2. array_sum() and array_column();
  3. array_sum() and array_map();
  4. array_reduce().

Using the foreach Loop

You can simply use the foreach loop to sum values of a specific key in an array, for example, in the following way:

// PHP 4+
$sum = 0;

foreach ($items as $item) {
    $sum += $item['qty'];
}

echo $sum; // 5

Although the code spans a few lines, this would be the fastest way as it's using the language construct foreach rather than a function call (which is more expensive).

Using array_sum() and array_column()

Using the array_column() function, you can get an array of all values from a single column in the input array:

// PHP 5.5+
$itemsQty = array_column($items, 'qty');

echo print_r($itemsQty); // Array ( [0] => 2 [1] => 3 )

You can then pass the resulting array as an argument to the array_sum() function to calculate and return the sum of all values in that array:

// PHP 5.5+
$itemsQty = array_column($items, 'qty');
echo array_sum($itemsQty); // output 5

Using array_sum() and array_map()

Using the array_map() function, you can apply a callback function to each element of the array. You can use this to simply return values of a specific key in the array:

// PHP 4+
function mapArray($item) {
    return $item['qty'];
}

$itemsQty = array_map('mapArray', $items);

echo print_r($itemsQty); // Array ( [item_1] => 2 [item_2] => 3 )

You can then pass the resulting array as an argument to the array_sum() function to calculate and return the sum of all values in that array:

// PHP 4+
function mapArray($item) {
    return $item['qty'];
}

$itemsQty = array_map('mapArray', $items);

echo array_sum($itemsQty); // 5

In PHP 5.3+, you may pass in an anonymous callback function directly to the array_map() function:

// PHP 5.3+
echo array_sum(array_map(function($item) {
    return $item['qty'];
}, $items)); // 5

In PHP 7.4+ you may use the shorter syntax with arrow functions:

// PHP 7.4+
echo array_sum(array_map(fn ($item) => $item['qty'], $items)); // 5

Using array_reduce()

You can iteratively sum values of a specific array key and reduce the result to a single value using the array_reduce() function, for example, like so:

// PHP 4+
function sumValues($carry, $item) {
    $carry += $item['qty'];
    return $carry;
}

echo array_reduce($items, 'sumValues'); // 5

In PHP 5.3+, you may pass in an anonymous callback function directly to the array_reduce() function:

// PHP 5.3+
echo array_reduce($items, function($carry, $item) {
    $carry += $item['qty'];
    return $carry;
}); // 5

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.