PDA

View Full Version : How to get the total of the values in one array


wert
05-23-2003, 12:18 AM
Hi there, I want to know if there is a function to ad each value in an array i mean something like this:

$My_Array[0] = 5;
$My_Array[1] = 5;
$My_Array[2] = 5;
$My_Array[3] = 5;
$My_Array[4] = 5;
$Total = Function_to_add_values_in_arrays($MyArray);
echo($Total);
//that should display 25

thnx

wert
05-23-2003, 12:27 AM
got it already, thnx any way

STDestiny
05-23-2003, 01:37 AM
Just out of curiosity, what was the answer? I was thinking sum($My_Array) but I could be wrong.

ASAAKI
05-23-2003, 03:29 PM
well there might already be something, but i'd just do it simply:

function addArrayVals($array){
$sum=0;
$no_vals=count($array);
for($i=0;$i<$no_vals;$i++){
$sum=$sum+$array[$i];
}
return($sum);
}

Weirdan
05-26-2003, 12:11 PM
array_sum($array);