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
0
votes
2 answers

How to sum the 2nd level values of a 2-dim array based on the 2nd level keys?

I have a multi-dimensional array like this: array( 0 => array( 11 => 18.000, 14 => 25.100, 15 => 5.000, 16 => 8.000, 19 => 21.600' ), 1 => array( 11 => 9.100, 12 =>…
Wasswa Samuel
  • 2,139
  • 3
  • 30
  • 54
0
votes
4 answers

php array_sum returning first value only

I'm fairly new at php, but this seems to be me overlooking something completely basic? I have some values in a database column, that are comma separated like so: 1,2,3 When I try to get the sum of the values, I expect the echo of array_sum to be 6,…
Mark
  • 33
  • 10
0
votes
3 answers

How to sum all of associative values of a multidimensional array?

How do I sum all the values of this associative array: Array ( [0] => Array ( [user1] => 20 ) [1] => Array ( [user2] => 30 ) [3] => Array ( [user3] => 10 ) ) Expected Output: 60 I tried, array_sum with no avail: $lsd = Array ( [0]…
0
votes
2 answers

Calculate totals for each column of a two dimensional array (matrix)

Given the array below, how can I create a sum array with matching keys? $arr = [ ['alpha', 1, 2, 3, 4, 5], ['beta', 1, 2, 3, 4, 5], ['gamma', 1, 2, 3, 4, 5], ['delta', 1, 2, 3, 4, 5] ]; This is what I eventually want: ['', 4, 8, 12,…
Ryan
  • 14,682
  • 32
  • 106
  • 179
-1
votes
2 answers

Sum of Two Arrays - Python

Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as…
YashuC ッ
  • 71
  • 1
  • 13
-1
votes
2 answers

PHP - How To Grouping if value same in array

This my array example Array ( [0] => Array ( [_id] => 5f76b1788ee23077dccd1a2c [product] => Array ( [0] => Array ( [_id] => 5d0391a4a72ffe76b8fcc610 ) ) [count] => 1 ) [1] => Array ( [_id] => 5f76b6288ee2300700cd1a3a [product] => Array ( [0] =>…
-1
votes
1 answer

multidimensional array sum and put into single column depending on key value

To-date and from-date has to be chosen. I have one array having key as date what i want is if i select 15 days key values up to 15 will be adding and come to one column so if my to-date and from-date difference is 30 so it will come two column 1-15…
Tann
  • 15
  • 1
  • 5
-1
votes
4 answers

want to add multidimensional array in php

I have two multi dimensional array.If the key is same then i want to get the sum how do i do that. one is- Array ( [2018-08-02] => Array ( [male] => 1 [female] => 0 ) [2018-08-07] => Array …
Tann
  • 15
  • 1
  • 5
-2
votes
3 answers

Why is this not printing the final output of sum?

Given an array of N integers. My task is to print the sum of all of the integers. Input: First line of input file contains a single integer T which denotes the number of test cases. For each test case, there will be two lines. First line contains N…
-2
votes
2 answers

Multi dimensional array, sum values of the same key

I have a multi dimensional array. I need to sum up the distance values with same 'driver' and 'date' keys Input : $firstArr = array( 0 =>array('driver'=>'xxxx', 'distance' => 100, 'vehicle' => 1, …
Sruthy
  • 47
  • 5
-2
votes
2 answers

how to sum element in array base key

i have array like this [1001] => Array ( [0] => 500 ) [1204] => Array ( [0] => 750 ) [1202] => Array ( [0] => 0 [1] => 10000 [2] =>…
Saeed Agha
  • 21
  • 2
-2
votes
2 answers

Minimum sum of two elements from two arrays such that indices are not same

Given two arrays ar1 and ar2, how can we find sum of two elements from two arrays such that indices are not same and their sum is minimum? However, I have written the following, but I am not sure regarding storing the sum in a temporary variable…
-2
votes
2 answers

How to replace subarrays with the sum of their elements?

Array ( [Apr] => Array ( [0] => 67 [1] => 392 [2] => 140 [3] => 250 ) [May] => Array ( [0] => 67 [1] => 392 [2] => 140 …
Rakesh Tripathi
  • 43
  • 1
  • 12
-2
votes
3 answers

How to get sum of values in specific index of multidimensional array using php?

I have this this array in my code Array ( [1] => Array ( [1] => 4 [5] => 7 ) [2] => Array ( [1] => 2 [5] => 5 ) [3] => Array ( [1] =>…
stand
  • 139
  • 1
  • 12
-3
votes
1 answer

Code for finding the maximum sum of a contiguous subarray fails a test case

Here is the problem - Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example: Given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6. Here is…
ANKUR SATYA
  • 219
  • 3
  • 10
1 2 3
4