array_sumCALCULATE the SUM of values in an ARRAY.
This function is compatible with LOCALE in PHP version 7.4.XX, however in PHP version 8.0.XX it is no longer supported.
This function returns the SUM of values as INTEGER or FLOAT.
This funtion returns ZERO if the $array is EMPTY.
<?php
nbr array_sum ( arr $array )
where,
$array = The input ARRAY
?>
$array
The input ARRAY.
EXERCISE
<?php
$arr01 = [ 2.876, 8.0, 18.91, 32.12 ];
echo 'The given ARRAY:<br>';
print_r($arr01);
echo '<br><br>The SUM: ';
setlocale(LC_NUMERIC, 'pt-pt', 'pt_PT');
$sarr01 = array_sum($arr01);
echo $sarr01 . '<br>';
?>
RESULT
PHP 7.4.XX
The given ARRAY:
Array ( [0] => 2.876 [1] => 8 [2] => 18.91 [3] => 32.12 )
The SUM: 61,906
ACCORDING THE BRAZILIAN LOCALE
PHP 8.0.XX
The given ARRAY:
Array ( [0] => 2.876 [1] => 8 [2] => 18.91 [3] => 32.12 )
The SUM: 61.906
NOT COMPATIBLE WITH LOCALE
EXERCISE
<?php
$arr02 = [ 'two point six hundred and forty-two',
'eight point three hundred and thirty-three',33.47 ];
echo 'The given ARRAY:<br>';
print_r($arr02);
echo '<br><br>The SUM: ';
$sarr02 = array_sum($arr02);
echo $sarr02 . '<br>';
?>
RESULT
The given ARRAY:
Array ( [0] => two point six hundred and forty-two [1] => eight point three hundred and thirty-three [2] => 33.47 )
The SUM: 33.47
EXERCISE
<?php
$arr03 = [TRUE,
false,
NULL,
2,
'3.44',
7.98034,
"SUM",
[2,4,6]];
echo 'The given ARRAY:<br><pre>';
print_r($arr03);
echo '</pre><br><br>The SUM: ';
$sarr03 = array_sum($arr03);
echo $sarr03 . '<br>';
?>
RESULT
The given ARRAY:
Array
(
[0] => 1
[1] =>
[2] =>
[3] => 2
[4] => 3.44
[5] => 7.98034
[6] => SUM
[7] => Array
(
[0] => 2
[1] => 4
[2] => 6
)
)
The SUM: 14.42034
EXERCISE
<?php
$arr04 = [ "continents" => ["South America", "Europe"],
"countries" => ["Brasil", "Portugal"]];
echo 'The given ARRAY:<br>';
print_r($arr04);
echo '<br><br>The SUM: ';
$sarr04 = array_sum($arr04);
echo $sarr04 . '<br>';
?>
RESULT
The given ARRAY:
Array ( [continents] => Array ( [0] => South America [1] => Europe ) [countries] => Array ( [0] => Brasil [1] => Portugal ) )
The SUM: 0
EXERCISE
<?php
$arr05 = [];
echo 'The given ARRAY:<br>';
print_r($arr05);
echo '<br><br>The SUM: ';
$sarr05 = array_sum($arr05);
echo $sarr05 . '<br>';
?>
RESULT
The given ARRAY:
Array ( )
The SUM: 0
EXERCISE
<?php
$i = 0;
while ($i++ < 1000) {
$a[] = $i;
$b[] = (string)$i;
}
$s1 = array_sum($a);
$s2 = array_sum($b);
print_r($s1);
echo '<br><br>';
print_r($s2);
echo '<br><br>';
var_dump($s1, $s2);
echo '<br><br>';
$j = 0;
while ($j++ < 100000) {
$c[] = $j;
$d[] = (string) $j;
}
$s3 = array_sum($c);
$s4 = array_sum($d);
print_r($s3);
echo '<br><br>';
print_r($s4);
echo '<br><br>';
var_dump($s3, $s4);
?>
EXERCISE
<?php
// array with integer values
$input = array(1, 2, 3, 4, 5);
echo "Integer array entries:<br>";
print_r($input);
echo '<br>The SUM: ';
var_dump( array_sum($input) );
echo '<br><br>';
// array with float values
$input = array(1.0, 2.2, 3.4, 4.6);
echo "Float array entries:<br>";
print_r($input);
echo '<br>The SUM: ';
var_dump( array_sum($input) );
echo '<br><br>';
// array with integer and float values
$input = array(1, 2.3, 4, 0.6, 10);
echo "Integer/Float array entries:<br>";
print_r($input);
echo '<br>The SUM: ';
var_dump( array_sum($input) );
?>
EXERCISE
<?php
$n = "10";
$n .= "0";
$nums = [&$n, 1000];
print_r($nums);
echo '<br><br>';
var_dump(array_sum($nums));
echo '<br><br>';
var_dump($n);
?>
EXERCISE
<?php
/*
* Testing array_sum() with different
* types of integer arrays containing
* data of following type:
* integer, octal, hexadecimal,
* maximum and minimum integer values
* & mixed of all integers
*/
// Int array
$int_values = array(3, 2, 100, 150, 25, 350, 0, -3, -1200);
echo "Sum of Integer array:<br>";
print_r($int_values);
echo '<br>The SUM: ' . array_sum($int_values);
// Octal array
$octal_values = array(056, 023, 00, 015, -045, 01, -07);
echo "<br><br>Sum of Octal array:<br>";
print_r($octal_values);
echo '<br>The SUM: ' . array_sum($octal_values);
// Hexadecimal array
$hex_values = array(0xAE, 0x2B, 0X10, -0xCF, 0X12, -0XF2);
echo "<br><br>Sum of Hex array:<br>";
print_r($hex_values);
echo '<br>The SUM: ' . array_sum($hex_values);
// Mixed values int, octal & hex
$mixed_int_value = array(2, 5, -1, 054, 0X3E, 0, -014, -0x2A);
echo "<br><br>Sum of mixed integer values:<br>";
print_r($mixed_int_value);
echo '<br>The SUM: ' . array_sum($mixed_int_value);
?>
EXERCISE
<?php
/*
* sum of array containing different float values
*/
// Simple float array
$float_input = array( 1.1, 2.3, 0.0, 0.5, -2.3, -0.8, .5);
echo "Simple float array:<br>";
print_r($float_input);
echo '<br>The SUM: ' . array_sum($float_input);
// float array with scientific notations
$float_input = array( 1.2e2, 23.4e3, -4.1e2, 0.2e2, 2.1e-2, .5e3);
echo "<br><br>Float array with scientific notations e:<br>";
print_r($float_input);
echo '<br>The SUM: ' . array_sum($float_input);
$float_input = array( 1.2E2, 23.4E3, -4.1E2, 0.2E2, 2.1E-2, .5E3);
echo "<br><br>Float array with scientific notations E:<br>";
print_r($float_input);
echo '<br>The SUM: ' . array_sum($float_input);
// Mixed float array
$float_input = array(
1.2,
0.5
-5.8,
6.334,
-0.65,
1.2e3,
-2.3e2,
5.56E3,
-3.82E-2
);
echo "<br><br>Mixed float array:<br>";
print_r($float_input);
echo '<br>The SUM: ' . array_sum($float_input);
?>
EXERCISE
<?php
/*
* Sum with integer and float array
* containing duplicate values
*/
echo "Array with duplicate values:<br><br>";
// integer array with duplicate values
$int_input = array( 2, 5, 7, 5, 0, -4, 2, 100);
echo "With integer array:<br>";
print_r($int_input);
echo '<br>The SUM: ' . array_sum($int_input);
// float array with duplicate values
$float_input = array( 2.3, 1.9, -4.1, 0.5, 1.9, -4.1, 3.6, 0.5);
echo "<br><br>With float array:<br>";
print_r($float_input);
echo '<br>The SUM: ' . array_sum($float_input);
?>
EXERCISE
<?php
/*
* Sum with 'input' having
* reference variables as elements
*/
$value1 = -5;
$value2 = 100;
$value3 = 0;
$value4 = &$value1;
// input array containing elements as reference variables
$input = array(
0 => 10,
1 => &$value4,
2 => &$value2,
3 => 200,
4 => &$value3,
);
echo "With elements as reference variables:<br>";
print_r($input);
echo '<br>The SUM: ' . array_sum($input);
?>
EXERCISE
<?php
/*
* Sum with associative array
* as 'input' argument
*/
echo "Testing with associative array:<br><br>";
// array with numeric keys
$input = array(0 => 1, 1 => 10, 2 => 0, 3 => -2, 4 => 23.56);
echo "With numeric keys:<br>";
print_r($input);
echo '<br>The SUM: ' . array_sum($input);
// array with string keys
$input = array('a' => 20, "b" => 50, 'c' => 0, 'd' => -30, "e" => 100);
echo "<br><br>With string keys:<br>";
print_r($input);
echo '<br>The SUM: ' . array_sum($input);
?>