Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Sum distinct arrays

Tags:

arrays

php

I have been trying all day to sum below array, based on the value and only get the distinct values.

Here is my input array:

[
    {
        "commoditycodes": "6204.4400",
        "statisticalvalue": 1152.6,
        "cvr": "DK35425950"
    },
    {
        "commoditycodes": "6204.4400",
        "statisticalvalue": 542.4,
        "cvr": "DK35425950"
    },
    {
        "commoditycodes": "6104.4300",
        "statisticalvalue": 1827,
        "cvr": "DK35425950"
    },
    {
        "commoditycodes": "6114.3000",
        "statisticalvalue": 5211.36,
        "cvr": "DK37968552"
    },
    {
        "commoditycodes": "6114.3000",
        "statisticalvalue": 798.64,
        "cvr": "DK99999999"
    }
]

I am trying to "gather" all arrays that have the same cvr value, but still show it distinct based on commoditycodes. Like this:

{
    "commoditycodes": "6204.4400",
    "statisticalvalue": 1695,
    "cvr": "DK35425950"
},
{
    "commoditycodes": "6104.4300",
    "statisticalvalue": 1827,
    "cvr": "DK35425950"
},
{
    "commoditycodes": "6114.3000",
    "statisticalvalue": 5211.36,
    "cvr": "DK37968552"
},
{
    "commoditycodes": "6114.3000",
    "statisticalvalue": 798.64,
    "cvr": "DK99999999"
}

]

As you can see, from the input, the first two results from my array have:

  • Identical commodity code
  • Identical CVR value

We then sum the statisticalvalue.

The third result have the same CVR value as the two first, however the commoditycode value is different - so this is an unique value.

The fourth and fifth elements of my arrays is both unique as well.

So basically, I am trying to sum statisticalvalue where:

  • CVR value is the same
  • Commodity code is the same

I have tried below:

$result = [];

foreach ($array as $val) {
    //Not a duplicate cvr code
    if (!isset($result[$val['cvr']])) {
        $result[$val['cvr']] = $val;
    //Duplicate cvr code.
    } else {
        $result[$val['cvr']]['statisticalvalue'] += $val['statisticalvalue'];

    }
}

But that returns below, which only take the unique cvr values - but not the commoditycodes:

[
    {
        "commoditycodes": "6104.6300",
        "statisticalvalue": 68701.56,
        "cvr": "DK35425950"
    },
    {
        "commoditycodes": "6104.6300",
        "statisticalvalue": 21108.95,
        "cvr": "DK37968552"
    },
    {
        "commoditycodes": "6114.3000",
        "statisticalvalue": 798.64,
        "cvr": "DK99999999"
    }
]

I am totally lost on how to check for this?

like image 689
oliverbj Avatar asked Jan 28 '19 14:01

oliverbj


Video Answer


1 Answers

Make an array with a complex index $val['cvr'].'|'.$val['commoditycodes']

$result = [];

foreach ($array as $val) {
    $index = $val['cvr'].'|'.$val['commoditycodes'];
    if (!isset($result[$index])) {
        $result[$index] = $val;
    //Duplicate
    } else {
        $result[$index]['statisticalvalue'] += $val['statisticalvalue'];
    }
}

If you want to make result array with numeric index, add after loop:

$result = array_values($result);
like image 51
splash58 Avatar answered Oct 04 '22 08:10

splash58