php method to add field values: 1. Use the array_column() function to return the value of a single column in the input array; 2. Use the array_sum() function to return the sum of the values in the array. .
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
How to add field values in php?
Add a field value in a PHP array (summation)
Use the array function: array_column() to return the value of a single column in the input array
Use the array sum function: array_sum() to return the sum of the values in the array
array(6) { [0]=> array(1) { ["pv"]=> string(4) "1.72" } [1]=> array(1) { ["pv"]=> string(4) "1.88" } [2]=> array(1) { ["pv"]=> string(5) "22.90" } [3]=> array(1) { ["pv"]=> string(4) "2.29" } [4]=> array(1) { ["pv"]=> string(4) "2.04" } [5]=> array(1) { ["pv"]=> string(4) "1.81" } }
Find the sum of the pv values in the array
$res = array_sum(array_column($att,'pv'));
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How to add field values in php. For more information, please follow other related articles on the PHP Chinese website!