Sum of a row of an associative array using PHP?

NickKampe picture NickKampe · May 18, 2010 · Viewed 17.1k times · Source

Is there a php function that returns the sum of a row of an associative array?

If not should I just use a counter and a foreach loop?

Appreciate it!

Answer

alex picture alex · Sep 4, 2014

To get the sum based on a certain column key, use this:

array_sum(array_column($assoc_array, 'key_name'));