Calculate the sum of values in an array

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

We can calculate the sum of array elements using the PHP predefined array_sum() function. This function takes the array name as its value and returns the sum of all the elements present within that particular array.

Example: PHP array_sum() function

In the given example, we have calculated the sum of all the array elements of the given array named $arr using the PHP predefined array_sum() function.

     

Sum of all the array elements is: 203

Apart from using a predefined PHP function, we can also calculate the sum of all the elements of an array using for loop.

Example: Using for loop

In the given example, we have calculated the sum of all the array elements of the given array $arr using for loop.

      print("Sum of all the elements is: " . $total); ?>  

Sum of all the array elements is: 203

Читайте также:  Other error in php

Conclusion

In this lesson, we have learned how to calculate the sum of all the array elements in PHP. We discussed two methods. At first, we have calculated the sum of all the array elements using the array_sum() function, and second we used the for loop for calculating the sum of all the elements of an array.

Источник

array_sum

array_sum() returns the sum of values in an array.

Parameters

Return Values

Returns the sum of values as an integer or float; 0 if the array is empty.

Examples

Example #1 array_sum() examples

$a = array( 2 , 4 , 6 , 8 );
echo «sum(a) color: #007700″>. array_sum ( $a ) . «\n» ;

$b = array( «a» => 1.2 , «b» => 2.3 , «c» => 3.4 );
echo «sum(b) color: #007700″>. array_sum ( $b ) . «\n» ;
?>

The above example will output:

User Contributed Notes 6 notes

If you want to calculate the sum in multi-dimensional arrays:

function array_multisum (array $arr ): float $sum = array_sum ( $arr );
foreach( $arr as $child ) $sum += is_array ( $child ) ? array_multisum ( $child ) : 0 ;
>
return $sum ;
>
?>

Example:

echo array_multisum ( $data );

Notably the function converts strings to float and ignores strings if they are not convertable:

$a = array( «String» , 2 , 4 , 6 , 8 );
echo «sum(a) keyword»>. array_sum ( $a ) . «\n» ;

$b = array( «12.3456» , 2 , 4 , 6 , 8 );
echo «sum(b) keyword»>. array_sum ( $b ) . «\n» ;
?>

sum(a) = 20
sum(b) = 32.3456

If you have a case where your array has int in strings, it sums them up as if there were only int in the array!
function sum_mix($a)
return array_sum($a);
>
var_dump(sum_mix([9, 3, ‘7’, ‘3’]));
Response will be int(22)

array_sum() doesn’t «ignore strings if they are not convertible», it converts them to zero. array_product() does the same thing, where the difference between «ignoring» and «converting to zero» is much more obvious.

//you can also sum multidimentional arrays like this;

function arraymultisum (array $arr ) $sum = null ;

foreach( $arr as $child ) $sum += is_array ( $child ) ? arraymultisum ( $child ): $child ;
>
return $sum ;
>

echo arraymultisum (array( 1 , 4 , 5 ,[ 1 , 5 , 8 ,[ 4 , 5 , 7 ]]));

array_sum converts strings to integer and array_sum(2,’2′) returns 4.

  • Array Functions
    • array_​change_​key_​case
    • array_​chunk
    • array_​column
    • 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_​is_​list
    • array_​key_​exists
    • array_​key_​first
    • array_​key_​last
    • array_​keys
    • array_​map
    • array_​merge_​recursive
    • array_​merge
    • array_​multisort
    • array_​pad
    • array_​pop
    • array_​product
    • array_​push
    • array_​rand
    • array_​reduce
    • array_​replace_​recursive
    • array_​replace
    • array_​reverse
    • array_​search
    • array_​shift
    • array_​slice
    • array_​splice
    • array_​sum
    • array_​udiff_​assoc
    • array_​udiff_​uassoc
    • array_​udiff
    • array_​uintersect_​assoc
    • array_​uintersect_​uassoc
    • array_​uintersect
    • array_​unique
    • array_​unshift
    • array_​values
    • array_​walk_​recursive
    • array_​walk
    • array
    • arsort
    • asort
    • compact
    • count
    • current
    • end
    • extract
    • in_​array
    • key_​exists
    • key
    • krsort
    • ksort
    • list
    • natcasesort
    • natsort
    • next
    • pos
    • prev
    • range
    • reset
    • rsort
    • shuffle
    • sizeof
    • sort
    • uasort
    • uksort
    • usort
    • each

    Источник

    array_sum() function in PHP

    The array_sum() function returns the sum of the values in an array. The returned value can be integer or float. It returns 0 if the array is empty.

    Syntax

    Parameters

    Return

    The array_sum() function returns the sum of values. The returned value can be integer or float. It returns 0 if the array is empty.

    Example

    The following is an example −

    Output

    The following is the output −

    Example

    Let us see another example −

    29.8,"b"=>66.4); echo array_sum($arr); ?>

    Output

    The following is the output −

    karthikeya Boyini

    I love programming (: That’s all I know

    • Related Articles
    • filter_has_var() function in PHP
    • filter_id() function in PHP
    • filter_input() function in PHP
    • filter_input_array() function in PHP
    • filter_list() function in PHP
    • filter_var_array() function in PHP
    • filter_var() function in PHP
    • constant() function in PHP
    • define() function in PHP
    • defined() function in PHP
    • die() function in PHP
    • eval() function in PHP
    • exit() function in PHP
    • get_browser() function in PHP
    • highlight_file() function in PHP

    Источник

    PHP array_sum() Function

    Return the sum of all the values in the array (5+15+25):

    Definition and Usage

    The array_sum() function returns the sum of all the values in the array.

    Syntax

    Parameter Values

    Technical Details

    Return Value: Returns the sum of all the values in an array
    PHP Version: 4.0.4+
    PHP Changelog: PHP versions prior to 4.2.1 modified the passed array itself and converted strings to numbers (which often converted them to zero, depending on their value)

    More Examples

    Example

    Return the sum of all the values in the array (52.2+13.7+0.9):

    Unlock Full Access 50% off

    COLOR PICKER

    colorpicker

    Join our Bootcamp!

    Report Error

    If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

    Thank You For Helping Us!

    Your message has been sent to W3Schools.

    Top Tutorials
    Top References
    Top Examples
    Get Certified

    W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

    Источник

    PHP array_sum() Function

    The array_sum() function is “used to calculate the sum of all the values in an array(one-dimensional and associative).” It returns 0 if the array is empty. The returned sum may be an integer or float.

    Syntax

    Parameters

    $array: It is an array whose sum needs to be calculated.

    Return value

    It returns the sum obtained after adding all the elements together.

    Example 1: How to Use array_sum() function in PHP

     $arr = [19, 21, 46]; echo array_sum($arr);

    PHP Array Sum Example

    Example 2: Using array_sum() function to calculate the average

    If you want to find an AVERAGE of the values in your array, use the sum and count the functions together. For example, let’s say your array is $arr, and you want the average.

     $arr = [19, 21, 46]; $average = array_sum($arr) / count($arr); echo $average; 

    PHP array_sum() Function Tutorial

    So, we can calculate the sum of values in an array using the array_sum() function.

    Example 3: Passing an empty array

    Empty Array

    Example 4: Integers and String Values in array_sum()

    If you pass the array containing both the strings and integers, the array_sum() Function returns a sum of integers. The strings are considered as 0.

     $arr = ['krunal', 21, 'ankit', 19]; echo array_sum($arr); 

    Источник

Оцените статью