I have multidimensional array and I want to sum all the same value with a certain condition.
Here's what my array looks like
$input_taxes = [ [ "tax_account_id" => 65, "checkbox" => false, "amount" => "13950.89", ], [ "tax_account_id" => 64, "checkbox" => 0, "amount" => "1920.70", ] ]; What I've researched, they used array_reduce. Below is my code:
$result = array_reduce($input_taxes, function ($carry, $item) { if (! isset($carry[$item['tax_account_id']])) { $chart_of_account = ChartOfAccount::where('id', $item['tax_account_id']) ->first() ->name; $carry[$item['tax_account_id']] = [ 'tax_account_id' => $item['tax_account_id'], 'amount_to_apply' => $item['amount_to_apply'], 'chart_of_account' => $chart_of_account ]; } else { $carry[$item['tax_account_id']]['amount_to_apply'] += $item['amount_to_apply']; } return $carry; }); Currently, it will sum all the same values even though it is not checked. Now I want to put a condition where it should be check first before it will sum all the same values.
NOTE: I already tried to put an if statement inside of both if and else statement but unfortunately it didn't work.
Question: How can I add a certain condition within array_reduce?
checkboxasfalse, so should the output be0?