tags:

views:

67

answers:

3

How can i add all of my array values together in PHP?

Is there a function for this or no?

Thank you.

+5  A: 

If your array consists of numbers, you can use array_sum to calculate a total. Example from the manual:

$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";

If your array consists of strings, you can use implode:

implode(",", $array);

it would turn an array like this:

strawberries
peaches
pears
apples

into a string like this:

strawberries,peaches,pears,apples
Pekka
+1  A: 

Hi

array_sum function should help. Here I presume your array comprises of integer or float values.

Andriyev
A: 

if your array are all numbers and you want to add them up, use array_sum(). If not, you can use implode()

ghostdog74