-1

i have an array. i want to check if there is duplicate FEE and if there is i want to sum up all the values with the same FEE COLUMN.

[12] => Array
    (
        [type] => Other Miscellaneous Fees
        [fee] => 158
        [amount] => -22.56
        [code] => COL_AUDIO
        [feedesc] => COLLEGE AUDIO VISUAL FEE
    )

[13] => Array
    (
        [type] => Other Miscellaneous Fees
        [fee] => 158
        [amount] => -297.86
        [code] => COL_AUDIO
        [feedesc] => COLLEGE AUDIO VISUAL FEE
    )

[14] => Array
    (
        [type] => Other Miscellaneous Fees
        [fee] => 182
        [amount] => -40.00
        [code] => STRAP
        [feedesc] => ID STRAP
    )

[15] => Array
    (
        [type] => Other Miscellaneous Fees
        [fee] => 177
        [amount] => -105.00
        [code] => PRISAA
        [feedesc] => PRISAA
    )

desired output, i want it to store in a new array. ive tried playing with array_sum and array colum. but it will sum all the fields. thanks

array_sum(array_column($feesArray, 'fee')); 

  [0] => Array
    (
        [type] => Other Miscellaneous Fees
        [fee] => 158
        [amount] => -320.42
        [code] => COL_AUDIO
        [feedesc] => COLLEGE AUDIO VISUAL FEE
    )

[1] => Array
    (
        [type] => Other Miscellaneous Fees
        [fee] => 182
        [amount] => -40.00
        [code] => STRAP
        [feedesc] => ID STRAP
    )

[2] => Array
    (
        [type] => Other Miscellaneous Fees
        [fee] => 177
        [amount] => -105.00
        [code] => PRISAA
        [feedesc] => PRISAA
    )
2
  • 1
    You probably don't want to "sum" up the elements, but get rid of elements with non unique fee. This is a basic manipulation which you should be able to do with a simple array regeneration with an if and a "master" array of "used" fees. Commented May 29, 2018 at 1:04
  • I have closed this page using another page with a superior answer. I wouldn't recommend the accepted answer to anyone, so at some point it would be better for researchers if this page did not exist. Separately, I have downvoted the question because there is no proof of effort, research, or even a broken coding attempt. Commented Sep 28, 2019 at 9:19

1 Answer 1

2

To SUM up all the values with the same FEE COLUMN you can create a for loop that iterates each result to check if the value is already in the array.

$data = Array(
    Array
    (
        "type" => "Other Miscellaneous Fees",
        "fee" => 158,
        "amount" => -22.56,
        "code" => "COL_AUDIO",
        "feedesc" => "COLLEGE AUDIO VISUAL FEE"
    ),
    Array(
        "type" => "Other Miscellaneous Fees",
        "fee" => 158,
        "amount" => -297.86,
        "code" => "COL_AUDIO",
        "feedesc" => "COLLEGE AUDIO VISUAL FEE"
    ),
    Array
    (
        "type" => "Other Miscellaneous Fees",
        "fee" => 182,
        "amount" => -40.00,
        "code" => "STRAP",
        "feedesc" => "ID STRAP"
    ),
    Array
    (
        "type" => "Other Miscellaneous Fees",
        "fee" => 177,
        "amount" => -105.00,
        "code" => "PRISAA",
        "feedesc" => "PRISAA"
    )
);


foreach ($data as $data_key => &$data_searcher) {
    foreach ($data as $data_searcher_key => $data_searcher_value) {
        if(
            ($data_searcher['fee'] === $data_searcher_value['fee']) &&
            $data_key !== $data_searcher_key
        ){
            $data_searcher['amount'] += $data_searcher_value['amount'];
            unset($data[$data_searcher_key]);
        }
    }
}


echo "<pre>";
print_r($data);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

5 Comments

how can i store this to a new array to restart the index of each item?
Not sure i understand what you are asking
why its only adding the first duplicate value in the array, the other values will not be sum up
I just ran a test, you are incorrect. The above code will combine all Array items by the FEE key field and add the amounts together.
i've tried to copy the last two elements in the array and run it but the output is not what im expecting? can you try it. its not working for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.