You need to couple it with array_map()
to select the f_count
column first:
array_sum(array_map(function($item) { return $item['f_count']; }, $arr));
Nowadays you can replace the inner function with array_column()
:
array_sum(array_column($arr, 'f_count'));
Of course, internally, this performs a double loop; it's just that you don't see it inside the code. You could use array_reduce()
to get rid of one loop:
array_reduce($arr, function(&$res, $item) { return $res + $item['f_count'];}, 0);
However, if speed is the only interest, foreach
remains the fastest:
$sum = 0;foreach ($arr as $item) { $sum += $item['f_count'];}
This is thanks to the "locality" of the variables that you're using, i.e. there are no function calls used to calculate the final sum.