php array sum array
How to sum all column values in multi-dimensional array?
How can I add all the columnar values by associative key? Note that key sets are dynamic.
19 Answers 19
If you want to get the total sum of all inner arrays with the same keys (the desired result that you’ve posted), you can do something like this (bearing in mind that the first inner array must have the same structure as the others) :
Here is a solution similar to the two others:
But this doesn’t need to check if the array keys already exist and doesn’t throw notices neither.
It can also be done using array_map :
Another version, with some benefits below.
I wanted to combine the best of Gumbo’s, Graviton’s, and Chris J’s answer with the following goals so I could use this in my app:
a) Initialize the ‘sum’ array keys outside of the loop (Gumbo). Should help with performance on very large arrays (not tested yet!). Eliminates notices.
b) Main logic is easy to understand without hitting the manuals. (Graviton, Chris J).
c) Solve the more general problem of adding the values of any two arrays with the same keys and make it less dependent on the sub-array structure.
PHP Array Sum Example | PHP array_sum() Function Tutorial
PHP Array Sum Example | PHP array_sum() Function Tutorial is today’s topic. It returns the sum of all the values in the array. It takes an array parameter and returns the sum of all the values in it. It returns the 0 if the array is empty. The returned sum may be integer or float.
PHP Array Sum Example
It calculates a sum of values in an array.
See the following syntax.
Now, see the following code example.
See the following output.
If you want to find an AVERAGE of the values in your array, use the sum and count the functions together. For example, let’s say your array is $arr and you want the average.
See the below output.
So, we can calculate the sum of values in an array using array_sum() function.
Empty Array
Let’s see the example where the array is empty.
Integers and String Values in array_sum()
If you pass the array containing both the strings and integers. Then the PHP array_sum() Function returns a sum of integers. The strings are considered as 0.
See the following output.
If you want to check if there are for example only strings in the array, you can use the combination of array_sum and array_map() as the following code.
Finally, PHP Array Sum Example | PHP array_sum() Function Tutorial is over.
php group by SUM using multi dimensional array
I have a php array like this:
So, how to get the SUM of time_spent based on group by url_id (using array_count_values)
3 Answers 3
$ts_by_url should now contain:
The OP posted an interesting problem, but the suggestion of using array_count_values() was not applicable; the function does not sum array values but counts their frequency. The answer of @marcocassisa works but is insufficient because of the unfortunate emission of E_NOTICES, albeit unrelated to any math issue. (An undefined variable is unset and thus of NULL value, but in a math context it will temporarily be coerced into a value of zero.) Modern PHP discourages the use of undefined variables by intentionally raising notices when code utilizes them, with one exception starting with PHP 5.1: a statement consisting of only an undefined variable; see here.
To rectify the aforementioned solution, one might use two foreach loops, one to set the index keys and initialize the array elements while the other would perform the summing of values, as follows:
While satisfactory, this solution lacks elegance. Attempting to combine the two foreach loops into one, will negatively impact the second element, as it gets defined and then needlessly redefined. One could code per the answer of @Charles and create the url id numbered index if it doesn’t already exist. Or, you could test to see if it is set, as follows:
Note: if an array element were defined as null, then isset() would return false while array_key_exists() would return true. Per, Ilia Alshanetsky
. if your application does not need to distinguish between an array key that does not exist and one whose value happens to be NULL you should use isset() because it happens to be a little faster.