vote up 0 vote down star

Hi all, I'm trying to count all the values inside an array, I've tried using count() but to no avail, the array values may also have negatives in them, example:

Array
(
    [name] => 1
    [phone] => 1
    [emailX] => 1
    [car] => 0
    [finance] => 2
    [employed] => 1
    [credit] => -5
)

If you count all of them together, the result should be 1, I'm just not sure how to go about it.. :/

flag

3 Answers

vote up 7 vote down check

You can use array_sum():

$sum = array_sum($myArray);

The count() function gives you the number of items in the array.

link|flag
vote up 2 vote down

count will return the number of elements, you will want to use array_sum

$sum = array_sum($arr);
link|flag
vote up 0 vote down

thanks so much, now i feel stupid for something so simple lol

link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.