Q:

How to calculate the sum of values in an array in PHP

0

How to calculate the sum of values in an array in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP array_sum() function

You can use the PHP array_sum() function to calculate the sum of all the numeric values in an array.

Let's try out the following example to understand how this function basically works:

<?php
$array1 = array(1, 2, 4.5, 8, 15);
$array2 = array("a" => 1.5, "b" => 2.5, "c" => 4.6, "d" => 10.4);
 
echo array_sum($array1); // Outputs: 30.5
echo "<br>";
echo array_sum($array2); // Outputs: 19
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to remove empty values from an array in PHP... >>
<< How to compare two array values in PHP...