PHP array_sum Function | Calculate Sum Elements in Array

PHP array_sum Function is an inbuilt Function in PHP. It calculates the sum of elements in an array. It takes in an array parameter and returns the sum of all values in the array. We will discuss the array_sum Function in PHP in this article. Also, we will discuss a few examples to demonstrate it’s usage.

Syntax

array_sum($array);

Parameters

The PHP array_sum Function takes in only one parameter. The function calculates the sum of elements of the input array. Obviously, the parameter is mandatory.

Return Value

The PHP array_sum Function returns the sum of elements of the input array. However, it returns 0 if the array is empty.

PHP array_map Function Diagram Concatly
PHP array_map Function Diagram

Examples

Let’s discuss a few examples to demonstrate the function.

Example 1: Integer Sum

<?php
	$testArray = array(1, 5, 10, 20, 30);
	$sum = array_sum($testArray);
	var_dump($sum);
	/*
	int(66)
	*/
?>

The array_sum Function is very simple to use. In the above example, it returns the sum of values of integer array.

Example 2: Float Array

Similarly, you can use this function to calculate the sum of a float array.

<?php
	$testArray = array(1.5, 5.1, 10, 20.8, 30.12);
	$sum = array_sum($testArray);
	var_dump($sum);
	/*
	float(67.52)
	*/
?>

Example 3: Empty Array

The Function returns 0 if the array is empty.

<?php
	$testArray = array();
	$sum = array_sum($testArray);
	var_dump($sum);
	/*
	int(0)
	*/
?>

Example 4: Integers and String Values

If you pass an array containing both strings and integers. Then the PHP array_sum Function returns the sum of integers. The strings are considered as 0.

<?php
	$testArray = array('My', 'Name', 1, 2, 3);
	print_r(array_sum($testArray));
	/*
	6
	*/
?>

Example 5: Calculating Average

Also, you can use this function along with count function to calculate the average of elements in the array.

<?php
	$testArray = array(1.5, 5.1, 10, 20.8, 30.12);
	$average = array_sum($testArray) / count($testArray);
	var_dump($average);
	/*
	float(13.504)
	*/
?>

Example 6: Using with array_map Function

If you want to check if there are for example only strings in an array, you can use a combination of array_sum and array_map like this:

<?php

function only_strings_in_array($arr) {
    return array_sum(array_map('is_string', $arr)) == count($arr);
}

$arr1 = array('one', 'two', 'three');
$arr2 = array('foo', 'bar', array());
$arr3 = array('foo', array(), 'bar');
$arr4 = array(array(), 'foo', 'bar');

var_dump(
    only_strings_in_array($arr1),
    only_strings_in_array($arr2),
    only_strings_in_array($arr3),
    only_strings_in_array($arr4)
);
?>
This will give you the following result:
bool(true)
bool(false)
bool(false)
bool(false)

The above example is taken from PHP.net. Additionally, you can learn about PHP array_map Function on Concatly.

Conclusion

In conclusion, you learned about the array_map Function in PHP. It is very simple to use and returns the sum of elements of an array. You can learn more about it on the Official Documentation of PHP.

You can learn about more Array Functions in PHP on Concatly!

Spread the Knowledge

Leave a Reply

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