1

I'm trying to sum 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 sum all of them together, the result should be 1, I'm just not sure how to go about it.

2 Answers 2

10

You can use array_sum():

$sum = array_sum($myArray);

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

$sum = array_sum($arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.