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
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys