Jump to: navigation, search

Php array sum

From w3cyberlearnings

Contents

PHP function array_sum

This function sums of all the array values.

Syntax array_sum

  • array: array input
array_sum(array);

Note

Use array_reduce() function to sum array values.

Example 1

<?php
$records = array('Mark'=>80,'JH'=>10,'Lilo'=>10);
$total = array_sum($records);

echo $total;
?>

Output

100

Example 2

<?php

$records = array('MATH100' => 80, 'MATH201' => 83, 'MATH203' => 78);

$total_math_score = array_sum($records);

echo $total_math_score;
?>

Output

241

Example 3: uses array_reduce to sum array values

<?php
function addup($v1, $v2) {
	return $v1 + $v2;
}

$records = array('MATH100' => 80, 'MATH201' => 83, 'MATH203' => 78);
$total_math_score =  array_reduce($records, 'addup');
echo $total_math_score;
?>


Output

241

Related Links


Navigation
Web
SQL
MISC
References