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

Array sum and count row in PHP

I have a function getData that returns: array:4 [▼ 0 => array:1 [▼ 0 => "5689.01" ] 1 => array:1 [▼ 0 => "5689.01" ] 2 => array:1 [▼ 0 => "0.0" ] 3 => array:1 [▼ 0 => "5665.11" ] ] I need to COUNT number of…
John Martin
  • 81
  • 1
  • 9
0
votes
3 answers

Easier way of doing the array_sum?

The code I am using works just fine for me. The problem I am currently having is more of a simpler method to do what I am trying to do. I created variables for every array that I need to sum into a final total value. Is there a way so I do not have…
0
votes
2 answers

Why PHP returns false in if condition when array_sum() value is 1?

I am facing one weird issue, sum of array is 1 but when I checking it into the IF condition it returns false. $array = array ( 0 => 0.237, 1 => 0.318, 2 => 0.215, 3 => 0.06, 4 => 0.069, 5 => 0.053, 6 =>…
Jimesh Gajera
  • 612
  • 1
  • 11
  • 32
0
votes
2 answers

array_sum numbers with different comma not equal to zero but long number

if(array_sum($request->amount) <> 0) { session()->flash('sum_error','sum_error'); return back(); } else return array_sum($request->amount); everything working fine with numbers like this 1 2 2 3 5 7 12 36 but when numbers in…
Awar Pulldozer
  • 1,071
  • 6
  • 23
  • 40
0
votes
1 answer

Sum multidimensional arrays values depending on other values within the array

After thinking and thinking I don´t find a way on how to solve this problem. I have been thinking of a loop inside a loop but I always reach to no end. Having multidimensional arrays like these: 0 => {'id' => (int) 1, 'meritos' => [ …
0
votes
0 answers

sum of array based on key(Group by user)

enter image description here This is my first array which i get from mysql Array ([0] => Array ( [created_by] => 4 [count] => 1 [total] => 300 ) [1] => Array ( [created_by] => 4 [count] =>…
Jegadeesh
  • 1
  • 3
0
votes
2 answers

array_sum from sql

I am fairly new to php so i'm looking for a bit of direction. I am query sql server and i need to do a sum on some of the columns. If i use the array 1,2,3 that works but i don't seem to be able to get the values for impressions_total. $sql =…
Rich Walker
  • 33
  • 10
0
votes
1 answer

PHP bad calculation in array_sum

I am trying to plus all array values like : $arr = array('55','55','99'); // I want to do 55 + 55 + 99 = X My function: function get_value_of_vouchers($id){ global $db; $prices = array(); $query = $db->query("SELECT * FROM vouchers WHERE…
Dani Prime
  • 59
  • 7
0
votes
4 answers

How to merge two arrays using column values and find sum of another column's values?

I am having two arrays with same keys from two different queries. First query result: Array ( [0] => Array ( [Contribution] => 1000.00 [P_Name] => A ) [1] => Array ( [Contribution] => 1500.00 [P_Name] => B …
user3615287
  • 19
  • 1
  • 3
0
votes
1 answer

Write a function to calculate total power conception of each devices as given in the array given in the code

new Page array('REFRIGERATOR' => array(3, 9, 7), 'WASHINGMACHINE' => array(2, 4, 2, 8)), 'TUESDAY' =>…
Jerin George
  • 7
  • 1
  • 2
0
votes
1 answer

array_sum counting all records in result query not each row individually

pulling hair out now I have a query that counts all relevant $price values in the array Basically the initial query checks the table to jobs that are completed but not invoiced The second query ( inside the initial query loop ) gets all the items…
Chris Yates
  • 65
  • 10
0
votes
2 answers

Sum hierarchical array for each level of array/hierarchy

I have an array, with a structure similar to that below. Where there is a heirarchy [2] => Array ( [sumW] => 200.39 [child] => Array ( [3] => Array ( …
Nertskull
  • 491
  • 6
  • 20
0
votes
4 answers

Merge multi-dimensional arrays by relating first columns and summing two other columns

I want to combine two multidimensional arrays in PHP. print_r($array_a): Array ( [0] => Array ( [0] => A [1] => 0 [2] => 1047 ) [1] => Array ( [0] => B [1] => 0 [2] => 279 ) [2] =>…
IkouKuhn
  • 89
  • 9
0
votes
4 answers

Sum column values from multiple arrays

I have an arrays with dynamic name. My array could be more than 3, depends and array variable should be unique $loopThrice = 3; $getSum = 0; $total = array(); $array0 = array(5, 10, 15, 20, 25, 30); $array1 = array(1, 2, 3, 4, 5, 6); $array2 =…
0
votes
3 answers

Merge indexed array of associative arrays and sum the values of recurring keys

I have a multidimensional array that looks like this: Array ( [0] => Array ( [GB] => 20827 [US] => 73 [ES] => 23 [AU] => 15 [DE] => 12 [MY] => 4 [QA] => 1 [VN] => 1 [SK]…