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

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

In this post we will give you information about How to calculate the sum of values in an array in PHP. Hear we will give you detail about How to calculate the sum of values in an array in PHPAnd how to use it also give you demo for it if it is necessary.

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
?>

 

Hope this code and post will helped you for implement How to calculate the sum of values in an array in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US