6

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?

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

This should work well if you don't need to preserve the format of your array elements.
What do you mean?
If he wanted to do aggregation for the elements with these two similar keys, he will need to preserve the same object structure, as he has shown in the snippet after that returns below. His code does not do that either though.
@splash58 this works beautifully! Such a "simple" solution. Thanks a lot!!! Really appreciate it.
Glad to help. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.