0

I have an array like below. There are id, label, cost, and cid in an array. We want to sum the cost based on the cid like for cid 22 cost should be 196.5 and for cid 11 cost should be 44.4. In our out put array we want to keep the label, cost (sum), and cid, Please see expected output array.

Array
(
    [0] => Array
        (
            [id] => 1331
            [label] => PM1
            [cost] => 98.25
            [cid] => 22
            [product_id] => 133
        )

    [1] => Array
        (
            [id] => 1332
            [label] => PM3
            [cost] => 22.20
            [cid] => 11
            [product_id] => 133
        )

    [2] => Array
        (
            [id] => 1341
            [label] => PM1
            [cost] => 98.25
            [cid] => 22
            [product_id] => 134
        )

    [3] => Array
        (
            [id] => 1342
            [label] => PM3
            [cost] => 22.20
            [cid] => 11
            [product_id] => 134
        )

)

Tried below

foreach ($array $key => $value) {

                         $final[$value['cid']] += $value['cost'];
                        } 

                       print_r ($final);

Getting below as an output

Array
(
    [22] => 196.5
    [11] => 44.4
)

Want expected output like below.

Array
    (
        [22] =>  Array
           (
            [label] => PM1
            [cost] => 196.5
            [cid] => 22
           )
        [11] => Array
           (
            [label] => PM3
            [cost] => 44.4
            [cid] => 11
           )
    )

Basically want to sum cost based on cid and want to keep the label, cost (sum), and cid.

Any help will be greatly appreciated.

3 Answers 3

1

I believe this should do the trick

$CIDs_identified = array();
$final_array = array();

foreach($original_array as $small_array){
    if(!in_array($small_array['cid'], $CIDs_identified)){
        $CIDs_identified[] = $small_array['cid'];
        $final_array[$small_array['cid']] = array(
            'label' => $small_array['label'],
            'cost' => $small_array['cost'],
            'cid' => $small_array['cid'],
        );
    }else{
        $final_array[$small_array['cid']]['cost'] += $small_array['cost'];
    
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your quick response, But the output is not as expected.
@skindia how come? I'm getting exactly the output you described as needed - 3v4l.org/3MKh9
Yes it is also correct , We made a typo mistake thank you so much. Accepted your solution also.
1

On this site, if you can provide your source array in usable format, that is really helpful so that we don't have to retype it. One way to do that is using var_export.

Below is a simple version. Create an array indexed by cid. If that item doesn't exist, populate it with the basic data and a zero cost. Then on each loop, including the initial, add the row's cost.

<?php
$data = [
    [
        'id' => 1331,
        'label' => 'PMI',
        'cost' => 98.25,
        'cid' => 22,
        'product_id' => 133,
    ],

    [
        'id' => 1341,
        'label' => 'PMI',
        'cost' => 98.25,
        'cid' => 22,
        'product_id' => 134,
    ],
];

$output = [];
foreach ($data as $item) {
    if (!isset($output[$item['cid']])) {
        $output[$item['cid']] = [
            'label' => $item['label'],
            'cost' => 0,
            'cid' => $item['cid'],
        ];
    }

    $output[$item['cid']]['cost'] += $item['cost'];
}

print_r($output);

Demo here: https://3v4l.org/MY6Xu

1 Comment

thanks @chris its working
0
$list = [
    [ 'id' => 1331, 'label' => 'PM1', 'cost' => 98.25, 'cid' => 22, 'product_id' => 133 ],
    [ 'id' => 1332, 'label' => 'PM3', 'cost' => 22.20, 'cid' => 11, 'product_id' => 133 ],
    [ 'id' => 1341, 'label' => 'PM1', 'cost' => 98.25, 'cid' => 22, 'product_id' => 134 ],
    [ 'id' => 1342, 'label' => 'PM3', 'cost' => 22.20, 'cid' => 11, 'product_id' => 134 ]
];

$result = [];

array_walk($list, function ($item) use (&$result) {
  if (isset($result[$item['cid']])) {
    $result[$item['cid']]['cost'] = $item['cost'] + $result[$item['cid']]['cost'];
  } else {
    $result[$item['cid']] = [ 'label' => $item['label'], 'cost' => $item['cost'], 'cid' => $item['cid'] ];
  }
});

print_r($result);

Output:

Array
(
    [22] => Array
        (
            [label] => PM1
            [cost] => 196.5
            [cid] => 22
        )

    [11] => Array
        (
            [label] => PM3
            [cost] => 44.4
            [cid] => 11
        )

)

Comments

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.