php

How to calculate sum of an array in PHP

In this tutorial, we are going to see how to calculate the sum of an array in PHP. 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
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More [st_adsense]

Leave a Reply

Your email address will not be published. Required fields are marked *