Home > OS >  How to sum values of repeated values in array with condtional statement php
How to sum values of repeated values in array with condtional statement php

Time:06-21

Using array_key_exists() you would find repeated values and be able to sum them up; but in my case, I am not trying to sum all the values at a go. What I am trying to do is to iterate through the values, find how many times they exist, and perform the following:

Assuming the values occur 4 times ( [ac_no] => 100001 as an example) in the array

  1. sum all the values

  2. sum the next 3 values

  3. sum the next 2 values

  4. leave last value since we can not sum single value

Expected result for [ac_no] => 100001 for instance:

1. .00   51255.11   -500.00   -621.05 = 52,376.05
2. 51255.11   -500.00   -621.05  = 52,376.05
3. -500.00   -621.05 = 1,120.94
4. -621.05 = 621.05



  

     Array
      (
        [0] => Array
            (
                [ac_no] => 100001
                [amount] => .00
            )
    
        [1] => Array
            (
                [ac_no] => 100001
                [amount] => 51255.11
            )
    
        [2] => Array
            (
                [ac_no] => 100001
                [amount] => -500.00
            )
    
        [3] => Array
            (
                [ac_no] => 100001
                [amount] => -621.05
            )
    
        [4] => Array
            (
                [ac_no] => 100002
                [amount] => .00
            )
    
        [5] => Array
            (
                [ac_no] => 100003
                [amount] => .00
            )
    
        [6] => Array
            (
                [ac_no] => 100004
                [amount] => 20714.00
            )
    
        [7] => Array
            (
                [ac_no] => 100004
                [amount] => .00
            )
    
    )

How can I perform the logic in the loop below?

foreach($nameAndCode as $key => $vals){

    if(array_key_exists($vals['ac_no'],$res)){

        $amt = abs($vals['amount']);
    
        $res[$vals['ac_no']]['ac_no']     = $vals['ac_no'];
        $res[$vals['ac_no']]['amount']    = $amt; 
       
    }
    else{
        $res[$vals['ac_no']]  = $vals;
    }

}

CodePudding user response:

Simple foreach() to do the job:

 $acNoWiseArray = [];
 
 foreach($array as $arr){
    $acNoWiseArray[$arr['ac_no']]['sum'][] = $arr['amount'];
 }
 
 
 $finalArray = [];
 foreach($acNoWiseArray as $key=>&$value){
    foreach($value['sum'] as $k=> $val){
        $finalArray[$key]['new_sum'][] = (is_array($value)) ? array_sum($value['sum']) : $value['sum'][$k];
        array_shift($value['sum']);
    }
   
    
 }
 
 print_r($finalArray);

https://3v4l.org/duoEc

CodePudding user response:

<?php
    $result = array();

    $nameAndCode = array(
        array(
            'ac_no' => 100001,
            'amount' => .00
        ),
        array(
            'ac_no' => 100001,
            'amount' => 51255.11
        ),
        array(
            'ac_no' => 100001,
            'amount' => -500.00
        ),
        array(
            'ac_no' => 100001,
            'amount' => -621.05
        ),
        array(
            'ac_no' => 100002,
            'amount' => .00
        ),
        array(
            'ac_no' => 100003,
            'amount' => .00
        ),
        array(
            'ac_no' => 100004,
            'amount' => 20714.00
        ),
        array(
            'ac_no' => 100004,
            'amount' => .00
        ),
    );


    // init new array of arrays to store each amount of ac_no seprately
    $parsedArr = array();

    foreach ($nameAndCode as $_arr) {
        // iterate over data source 
        if (!isset($parsedArr[$_arr['ac_no']])) $parsedArr[$_arr['ac_no']] = array();
        // save to new, convenient array
        array_push($parsedArr[$_arr['ac_no']], $_arr['amount']);
    }

    // iterate over parsed array where each element is an array of values
    foreach ($parsedArr as $ac_no => $values) {
        // check if index in resulting array exist
        if (!isset($result[$ac_no])) $result[$ac_no] = array();
        // prep all_values sum index
        if (!isset($result[$ac_no]['ALL_VALUES'])) $result[$ac_no]['ALL_VALUES'] = 0;

        // iterate over each values in ac_no array; set iterator to current key and itarate at max 3 elements ahead
        foreach ($values as $key => $val) {
            for ($i = $key; $i < $key   4; $i  ) {
                // check if element exists
                if (!isset($result[$ac_no][$key])) $result[$ac_no][$key] = 0;
                // check if element exists
                if (isset($values[$i])) $result[$ac_no][$key]  = abs($values[$i]);
            }
            $result[$ac_no]['ALL_VALUES']  = abs($val);
        }
    }
    print_r($result);
  •  Tags:  
  • php
  • Related