How to get the sum of the array of specific value

sum of an array javascript
sum of elements in an array c++
c program to find sum of array elements
sum of array java
javascript sum array of objects
javascript sum of numbers
sum of an array python
find a pair of elements from an array whose sum equals a given number in java

I have the following array

Array
(
    [0] => Array
        (
            [gender] => F
            [thisweektotal] => 3
        )

    [1] => Array
        (
            [gender] => M
            [thisweektotal] => 2
        )

    [2] => Array
        (
            [gender] => Adult
            [thisweektotal] => 1
        )

    [3] => Array
        (
            [gender] => Young Adult
            [thisweektotal] => 4
        )

) 

I want the sum of thisweektotal of the gender M and F only that is 5 I have tried the below code but i am not getting sum of all the gender F and I want only the sum of the genders M and F

foreach($analytics_data['this_week'] as $value) 
  {

    if($value['gender']=='M' || $value['gender']=='F')
    {
      $total_this_week_arr = array_column($value['thisweektotal'], 'thisweektotal');
      $this_week_total = array_sum($total_this_week_arr);

    }

  }
 echo "this_week_total=".$this_week_total;

Since you are looping, you dont need the array_column and array_sum. You can just:

$this_week_total = 0;

foreach($analytics_data['this_week'] as $value)  {
    if( $value['gender'] == 'M' || $value['gender'] == 'F' )
    {
      $total_this_week_arr = $value['thisweektotal'];
      $this_week_total += $total_this_week_arr ;
    }
}

echo "this_week_total = " .$this_week_total;

Array sum in C++ STL, How do you find the sum of an array element in C++? Write a JavaScript function to calculate the sum of values in an array. Test Data: Omits the key-value pairs corresponding to the given keys from an object.


You're looping through the array just you need a variable which holds the sum value

$sum = 0;//Your variable which stores the value of sum

foreach($analytics_data['this_week'] as $k=>$arr){
    if($arr['gender']=='F' || $arr['gender']=='M')
    $sum+=$arr['thisweektotal']; //Adding sum to $sum
}

echo $sum;

How To Find All Pairs of Elements In An Array Whose Sum Is Equal , How do you find all pairs of an integer array whose sum is equal to a given number? S = sum(A) returns the sum of the elements of A along the first array dimension whose size does not equal 1. If A is a vector, then sum(A) returns the sum of the elements. If A is a matrix, then sum(A) returns a row vector containing the sum of each column. If A is a multidimensional array, then sum(A)


Here's a solution using array_column with the third paramter which works because you don't have conflicting values in the "gender" key transforming the array to a much simplier structure for summing.

$gender = array_column( $array, 'thisweektotal', 'gender' );
$total = $gender['M'] + $gender['F'];

If there is a chance of a missing key we can try using array_intersect_key as a sort of a "whitelist filter" to your array...

$gender = array_column( $array, 'thisweektotal', 'gender' );
$filtered = array_intersect_key( $gender, array_flip( array( 'M', 'F' ) ) );
$total = array_sum( $filtered );

Given an array A[] and a number x, check for pair in A[] with sum as x , whose sum is equal to the given value. bool hasArrayTwoCandidates( given sum" ;. else Duration: 10:35 Posted: Oct 10, 2011 After my array gives me random values, I want to be able to get the sum of those values. The method I'm using now isn't working, it gives me an incorrect calculation.


$aInput = [
    [
        "gender" => "F",
        "thisweektotal" => 3,
    ], [
        "gender" => "M",
        "thisweektotal" => 2,
    ], [
        "gender" => "Adult",
        "thisweektotal" => 1,
    ], [
        "gender" => "Young Adult",
        "thisweektotal" => 4,
    ]
];

$iSum = array_reduce($aInput, function($sum, $el) {
    if (in_array($el['gender'], ['F', 'M'])) {
        $sum += $el['thisweektotal'];
    }
    return $sum;
});

var_dump($iSum);

How to sum up specific array elements? - JavaScript, How to sum up particular elements of an array that will equal a you may find the sum by iterating through the array, take the value for the  Otherwise, the formula must be entered as a legacy array formula by first selecting the output cell, entering the formula in the output cell, and then pressing CTRL+SHIFT+ENTER to confirm it. Excel inserts curly brackets at the beginning and end of the formula for you.


How to find the sum of an array of numbers, Recommended (reduce with default value). Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the  >>>counts = {int(value): list(y).count(value) for value in set(y)} >>>print(counts) {0: 8, 1: 4} This will create a dictionary with the values in your ndarray as keys, and the counts of the values as the values for the keys respectively.


How to calculate sum of specific array elements, Let's say you have two arrays of the same size: one with the data (x) and one carrying some indices that characterize the data (q). For example: x = [ 3 5 2 10 6 4]  Is it possible to sum an array (from a specific value)? Right now I am using a get items action for SP, then I put the array returned into a variable that I then use a compse action to find the length (so I can count the items returned). Is it possible to sum values from a field in that get items?


array_sum - Manual, If you want to find the AVERAGE of the values in your array, use the sum and count functions together. For example, let's say your array is $foo and you want the  If more than one combination of values has a sum equal to the specific value, only one combination is listed. Find and list all combinations that equal a given sum quickly and easily in Excel Kutools for Excel 's Make Up A Number utility can help you to find and list all combinations and specific combinations that equal to a given sum number