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
1 answer

How can i sum of values in wordpress from ACF field?

I have a query and i can't sum value in array. My WP query 'car', 'post_status' => 'publish', …
Sirvav
  • 3
  • 4
0
votes
0 answers

Find the maximum sum among all segments formed after removing one element in every iteration

An array A of size N having positive integers is given. Another array B having permutation of numbers 1 to N is given. Your task is to perform N steps : At i-th step, you have to remove element A[B[i]] from array A. After removing the elements,…
user972404
  • 11
  • 3
0
votes
3 answers

Sum array values of a column within each column of an array with 3 levels

I'm trying to use array_sum() on columns within columns of a multidimensional array. For eg: I have an array that looks like this: $array = [ [['value' => 1100], ['value' => 1000], ['value' => 3000]], [['value' => 1200], ['value' => 2000],…
Ali
  • 1,326
  • 1
  • 17
  • 38
0
votes
5 answers

How to solve a partition problem using recursion only

I got a partition problem for which I need advice. I'm given a 1D array whose length is even. I need to write a boolean method to determine whether the array can be divided into 2 equally sized subarrays with equal sum, no loops are allowed. For…
Sigma9
  • 27
  • 1
  • 6
0
votes
1 answer

Count specific values of PHP Array that has no column assignments

I need to display a count of the no of car makes. $array = array( array(1,'Benz'), array(3,'Toyota'), array(4,'Mazda'), [2,'VW'], [5,'Mazda'] ); $count =…
user389391
  • 97
  • 1
  • 8
0
votes
2 answers

Summarise values in multidimensional array with condition in php/mysql

I have the following table in mysql e_id p_id w1 w2 w3 w4 87 1019 1 1 0 0 87 1019 0 0 0 1 87 1020 1 1 1 1 87 1021 0 1 0 0 87 1021 0 0 1 1 87 1021 1 0 0 0 89 1020 1 1 1 1 89 1022 1 1 1 0 89 1022 0 0 0 1 I want to sum rows…
user2867504
  • 95
  • 1
  • 9
0
votes
2 answers

how to add multidimensional array values ​whose values ​are from different arrays

I have been fighting with this all day and would like another set of eyes to maybe give me some insight. I'm not sure I am even approaching this the correct way. I have two arrays, namely a multidimensional array and a single array, I am trying to…
salman
  • 27
  • 7
0
votes
1 answer

Format number in array within an array_sum

I have a view in a laravel project where I have to print the sum of some array columns. I Have this kind of code to achieve the result: @foreach($data as $row) ....... @foreach($row as $key => $value) @if(in_array($key, ['PARZ. IN', 'PARZ.…
DannyV
  • 9
  • 3
0
votes
2 answers

PHP a way to find the key after array_sum

I'm working on SMA(simple moving average) function for 5 second ( find the best average of every 5 values for one by one from an array). I have over 1000 arrays as a result but here we only have 2 for instance. 0 => array:5 [▼ 1608616431…
Ehsan Pro
  • 15
  • 3
0
votes
3 answers

Group rows of data and in each group sum one column and concatenate another column

In php, I have an indexed array of associative rows like this: $the_array = [ ['id' => 1, 'value' => 10, 'name' => 'apple'], ['id' => 1, 'value' => 20, 'name' => 'orange'], ['id' => 1, 'value' => 30, 'name' => 'banana'], ['id' => 2,…
0
votes
2 answers

Combine 2 PHP arrays to create a leaderboard

I have 2 arrays - Dealerships and Scores: Dealerships Array( [0] => Test Dealership 2 [1] => Test Dealership [2] => Test Dealership [3] => Test Dealership 3 [4] => Test Dealership ) Scores Array( [0] => 0 [1] => 427 …
Lee Harris
  • 13
  • 4
0
votes
2 answers

PHP array_sum - why array_sum is not working for the following script?

I have seen read other posts that are almost similar to my problem. but I can't find the right answer to my problem. here I have a $data[4] array that is like the following. Array ( [0] => 0.84 ) Array ( [0] => 2.79 ) and this my php code for…
yagami cell
  • 147
  • 12
0
votes
2 answers

sum values in sub array

this is how I store values to array $array[$row['name']][] = $row['from'] - $row['to']; and here is result Array ( [name1] => Array ( [0] => 1 [1] => 12 [2] => 10 ) ) Array ( [name2] => Array ( …
user10118798
0
votes
1 answer

Sum values that date_diff function returns

I need to sum all values that date_diff function returns. I have tried using array_sum(), but I get 0 as a result (as it is empty). The result should be 14. $sql = "SELECT start_date, end_date FROM events"; $result = $conn->query($sql); if…
Ricky97
  • 79
  • 8
0
votes
3 answers

array_sum PHP wrong result

the result of this should be zero! echo array_sum([-61.50,50.00,10.50,1.00,0.00,50.00,-16.73,-20.00,-55.75,42.48]); Why is giving -7.105427357601E-15?