0

I'm not sure how to formulate the title of my question. I have this array:

Array
(
    [0] => Array
        (
            [account] => 700000
            [percent] => 0.0000
            [amount] => 3
        )

    [1] => Array
        (
            [account] => 705010
            [percent] => 6.0000
            [amount] => 4.7
        )

    [2] => Array
        (
            [account] => 700000
            [percent] => 0.0000
            [amount] => 93
        )

    [3] => Array
        (
            [account] => 700000
            [percent] => 6.0000
            [amount] => 9.43
        )

    [4] => Array
        (
            [account] => 700000
            [percent] => 12.0000
            [amount] => 35.72
        )

    [5] => Array
        (
            [account] => 700000
            [percent] => 21.0000
            [amount] => 8.26
        )

    [6] => Array
        (
            [account] => 705300
            [percent] => 21.0000
            [amount] => 8.26
        )

    [7] => Array
        (
            [account] => 705300
            [percent] => 21.0000
            [amount] => 57.86
        )

)

I need to find a way to add up the amount if both account and percent match.

Should I loop over each array and do "if" checks to see if the account and percent already exist, then add up amount, else leave it as it is?

Is there a php function for this?

I have been looking into array_unique but I'm not quite sure how to do it like that.

As always, any help is appreciated!

5
  • 8
    No, there is no function for this. PHP is a toolbox with some basic hammers, screwdrivers, and occasional swiss army knife. It will never have the_function_that_does_some_complex_operation_that_only_one_person_in_the_universe_needs(). so get busy with foreach() and if() Commented Aug 10, 2015 at 20:19
  • The simple way I could suggest you that, before adding new array value to your existing array loop over each index, if value is found just update with new amount and if not then add it to that array. I think your are also thinking same manner to deal this problem. Commented Aug 10, 2015 at 20:31
  • @maytham, I need to check if both account and percent are already existing. If they match than add the amount to the previous amount. In the example it would mean the last 2 arrays are counted like this: Account = 705300, percent = 21, amount = 66.12 (8.26 + 57.86) Commented Aug 10, 2015 at 20:51
  • If there are 5 account AND percent the same I need to add up the amount of the 5 matching arrays Commented Aug 10, 2015 at 21:00
  • I already have this: foreach ($logicalarray as $array){ if(in_array($array['account'], $array) && in_array($array['percent'], $array)){ $amount += $array['amount']; $newarray = array('account' => $array['account'], 'percent' => $array['percent'], 'amount' => $amount); } } print_r($newarray); But it just adds up every amount Commented Aug 10, 2015 at 21:27

1 Answer 1

1

You could use something like this. It will generate a nested structure that seems like it would probably be easier to work with.

foreach ($array as $item) {
    $grouped[$item['account']][$item['percent']] += $item['amount'];
}

If you need to put them back into the original format, just do basically the opposite operation

foreach ($grouped as $account => $percents) {
    foreach ($percents as $percent => $amount) {
        $new_array[] = array(
            'account' => $account, 'percent' => $percent, 'amount' => $amount);
    }
}
Sign up to request clarification or add additional context in comments.

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.