Questions tagged [array-sum]

A PHP function that calculate the sum of values in an array and return result as an integer or float or 0 if the array is empty.

The array_sum() calculate the sum of values in an array and return result as an integer or float or 0 if the array is empty. .

Examples

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

$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "\n";
?>

The above example will output:

sum(a) = 20
sum(b) = 6.9
60 questions
23
votes
10 answers

multidimensional array array_sum

I have seen various posted about this question so am aware some answers to this may exist. however I am none the wiser after reading these. I have an array that is like the following. [0] => Array ( [id] => 95659865986 …
Mr Sorbose
  • 777
  • 4
  • 20
  • 34
4
votes
3 answers

PHP Array_Sum on multi dimensional array

If I have a multi dimensional array in PHP like so... [0] => Array ( [url] => http://domain1.com [domain] => domain1.com [values] => Array ( [character_length] => 25 …
michael
  • 4,427
  • 6
  • 38
  • 57
3
votes
6 answers

Finding smallest value from array in java

I am trying to get the sum, average, max and min value from user input in array. sum, average and max is giving the correct output. But min value is not working. Where am I doing wrong would someone help me please to find out? import…
Rashed Hasan
  • 3,721
  • 11
  • 40
  • 82
3
votes
3 answers

Putting values into an array and using array_sum

ok im a little stuck, i know its a simple thing im missing here so hoping fresh eyes will help I have values in a column stored as 2:7:99 etc each value is seperated by : Now I can seperate all the values out and query another table to get the…
Chris Yates
  • 65
  • 10
2
votes
2 answers

PHP array_sum not adding sqlsrv query

I am performing the following SQLSRV Query using PHP 5.6 & 7.0.29 and when I get the results I can not seem to add (SUM) the total results of "grand_total" using array_sum or even when I try to loop it. $sql = "SELECT inv_trx.trx_date,…
Stephen
  • 23
  • 3
2
votes
3 answers

PHP sum array values by item.qty

I wonder if there is a faster way to sum i.e. the weight of each item by qty. $items = [ [ 'qty' => 1, 'weight' => 1, ], [ 'qty' => 2, 'weight' => 1, ], [ 'qty' => 3, …
cottton
  • 1,522
  • 14
  • 29
1
vote
1 answer

Calculating sub array sums with a Fenwick tree

I have a problem and I don't know if I can solve it with a Fenwick tree. Here's the problem: I have an original array a = [8,4,7,0]. Now I iterate through the array and at each step I am interested in the sorted sub-arrays, which look like this:…
1
vote
3 answers

Add the sum of an undefined array in Javascript

I now this code works if I have preset values defined in my array, how would I go about doing this when the values are not defined? I have a hard time with all loops in general especially for loop. I know there are methods to achieve this, but for…
gears244
  • 63
  • 1
  • 6
1
vote
1 answer

Simple php array sum not working (add's up to zero)

I have an auto generated array liek so.. (looks like real data but has been anonymized).. Array ( [16280] => Array ( [0] => 16280 [1] => 19-04696-34480 [2] => Helen*smith [3] => helen…
Glen Keybit
  • 296
  • 2
  • 15
1
vote
1 answer

How to get sum of array values according to some index (non-sorted)

I want the sum of monthly_rental and arc if the month and full_name are the same in one array. Input Array Array ( [0] => Array ( [0] => Array ( [id] => 521 [full_name]…
1
vote
4 answers

Values of keys in different arrays

So I have a query that returns the following results in PHP, and I'm trying to add the total of the hours_played, I have tried array_sum() but that does not seem to work, I would like to be able to add the values of hours_played to be 11 that way I…
ImANobody
  • 29
  • 4
1
vote
3 answers

Sum array values excluding value of element with specific key

If I have: $my_array = array( "user1" => "100", "user2" => "200", "user3" => "300" ); How can I use array_sum to calculate the sum of the values except the one of the user3? I tried this function (array_filter), but it did not…
Sara Z
  • 625
  • 7
  • 18
1
vote
2 answers

Merge and sum multiple multidimensional associative arrays

I'm needing a way to merge several arrays ( probably around 8 ) and sum any duplicate keys or sub-keys. For example: $arr1 = [ "Friday" => ["Breakfast" => 32, "Lunch" => 45], "Sunday" => ["Lunch" => 12] ]; $arr2 = [ "Sunday" =>…
Adam
  • 9,189
  • 15
  • 46
  • 62
1
vote
4 answers

How to Sum Columns of a Multi-Dimensional Array?

I have array data like : [ 'A1' => [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'A2' => [1, 1, 0, 0, 0, 0, 0, 0, 0, 0], 'A3' => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; I want to add the column values and produce a flat array of sums. I want answer like…
jaya
  • 327
  • 3
  • 4
  • 16
0
votes
3 answers

Loop invariant of a running sum array?

So I was given this algorithm : Algorithm RunningSum(int[] A) n = A.length; for i = 2 to n do A[i] = A[i] + A[i - 1] end return A; end and I need to find the loop invariant but I can figure out what the output could be... lets say I have a…
Klea
  • 15
  • 6
1
2 3 4