You are here: Home / Topics / How to calculate sum of an array in PHP

How to calculate sum of an array in PHP

Filed under: PHP on 2023-07-02 21:16:20

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

How to calculate sum of an array in PHP
 

<?php
 $arr1 = array(1, 2, 3);
 $arr2 = array("a" => 1, "b" => 2, "c" => 3);
 echo array_sum($arr1). '<br />';
 echo array_sum($arr2); 
?>
 

Output:

6
6

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.