Home > Back-end >  Array sum with conditional in php
Array sum with conditional in php

Time:03-14

I have the following problem and I am stuck.

I have two arrays, in the first one I have two very important main indexes each TS00X are codes and float its value. And in the second array I have all the Codes that has as value zero.

Array 1

array (size=2)
  1 => 
    array (size=11)
      'TS001' => float 585.81
      'TS002' => float 3110.1
      'TS003' => float 8763
      'TS004' => float 14497.71
      'TS005' => float 16695.24
      'TS006' => float 8178.68
      'TS007' => float 15878.4
      'TS008' => float 4140.36
      'TS009' => float 3196.96
      'TS010' => float 1776.3
      'TS011' => float 1916.16
  2 => 
    array (size=11)
      'TS001' => int 0
      'TS002' => int 0
      'TS003' => int 0
      'TS004' => int 0
      'TS005' => int 0
      'TS006' => float 11906.4
      'TS007' => float 22636.8
      'TS008' => float 5798.52
      'TS009' => int 0
      'TS010' => float 2250.9
      'TS011' => float 2419.992

Array 2:

array (size=6)
  0 => string 'TS001' (length=10)
  1 => string 'TS002' (length=10)
  2 => string 'TS003' (length=10)
  3 => string 'TS004' (length=10)
  4 => string 'TS005' (length=10)
  5 => string 'TS009' (length=10)

The logic would be that all TS00X that has float 0 will be excluded from the summation of any index of the first array.

So: All the codes that are in the second array will be excluded from the summation of the first array in each index.

That is, the summation of index 1 of the first array would have to give: 31,889.9, and for the second index of the first array it would be: 45,012.612.

The desired result would be an array with the same indexes of the first one, but with the correct values, like this:

array (size=2)
  1 => float => 31.889,9 
  2 => float =>45.012,612

The index of this array is important and goes according to Array 1.

I have tried many things including this code, but the summation is always zero for the second index!

foreach ($array1 as $idx=> $array_value) {
                foreach ($array_value as $code => $float) {
                    foreach ($array2 as $code2) {
                        if (empty($sum[$idx])) {
                            $sum[$idx] = 0;
                        }
                        $sum[$idx]  = $float;
                    }
                }
            }

CodePudding user response:

$array1 = [
    [
        'TS001' => 585.81,
        'TS002' => 3110.1,
        'TS003' => 8763,
        'TS004' => 14497.71,
        'TS005' => 16695.24,
        'TS006' => 8178.68,
        'TS007' => 15878.4,
        'TS008' => 4140.36,
        'TS009' => 3196.96,
        'TS010' => 1776.3,
        'TS011' => 1916.16
    ],
    [
        'TS001' => 0,
        'TS002' => 0,
        'TS003' => 0,
        'TS004' => 0,
        'TS005' => 0,
        'TS006' => 11906.4,
        'TS007' => 22636.8,
        'TS008' => 5798.52,
        'TS009' => 0,
        'TS010' => 2250.9,
        'TS011' => 2419.992
    ]
];

$array2 = [ 'TS001', 'TS002', 'TS003', 'TS004', 'TS005', 'TS009' ];

$result = [];
foreach ($array1 as $index => $item) {
  $result[$index] = array_sum(array_filter($item, function ($value, $key) use ($array2) {
    return !in_array($key, $array2, true);
  }, ARRAY_FILTER_USE_BOTH));
}

print_r($result);

Output:

Array
(
    [0] => 31889.9
    [1] => 45012.612
)
  • Related