php - How can I add all the values in an array? -
php - How can I add all the values in an array? -
i have array (or object?) looks this:
array ( [0] => 5 [1] => 4 [2] => 3 [3] => 4 [4] => 4 [5] => 4 [6] => 4 [7] => 3 [8] => 4 [9] => 5 [10] => 3 [11] => 4 [12] => 4 [13] => 4 [14] => 3 [15] => 4 [16] => 4 [17] => 5 ) how add together values within it. ie. 5 + 4 + 3 etc... ideas?
what have array, not object.
you can iterate on array using loop (like foreach), , add together values variable.
$total = 0; foreach($array $val) $total += $val; or utilize core function array_sum().
array_sum($array); careful sec one, because if there float value in array, , expect integer value returned, function homecoming float.
php arrays sum
Comments
Post a Comment