I'm trying to combine two arrays and subtract a value within the array if two keys match each other.
This is the code I've written so far.
$dat = array();
for ($i = 0; $i <= (count($cashdrawer_sales) - 1); $i++) {
$obj = $cashdrawer_sales[$i];
foreach ($cashdrawer_refund as $k=>$ref) {
if ($obj['cash_drawer']==$ref['cash_drawer'] && $ref['department_id']==$obj['department_id']) {
$cash_drawer_total = $obj['cash_drawer_sale_total'] - $ref['cash_drawer_refund_total'];
$arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total);
array_push($dat,$arr);
} else {
$cash_drawer_total = $obj['cash_drawer_sale_total'];
// echo $cash_drawer_total."<br>";
$arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total);
array_push($dat,$arr);
}
}
}
print_r($dat);
Here's an example of the $cashdrawer_sales and $cashdrawer_refund arrays that I'm wanting to manipulate.
cashdrawer_sales
Array
(
[0] => Array
(
[department_id] => 80000
[cash_drawer] => 21112
[cash_drawer_sale_total] => 64.00
)
[1] => Array
(
[department_id] => 80000
[cash_drawer] => 21117
[cash_drawer_sale_total] => 15.00
)
[2] => Array
(
[department_id] => 80000
[cash_drawer] => No Cash Drawer
[cash_drawer_sale_total] => 50.00
)
[3] => Array
(
[department_id] => 50502
[cash_drawer] => 21112
[cash_drawer_sale_total] => 193.00
)
[4] => Array
(
[department_id] => 50502
[cash_drawer] => 21113
[cash_drawer_sale_total] => 30.00
)
[5] => Array
(
[department_id] => 50502
[cash_drawer] => 21117
[cash_drawer_sale_total] => 10.00
)
[6] => Array
(
[department_id] => 50502
[cash_drawer] => No Cash Drawer
[cash_drawer_sale_total] => 80.00
)
[7] => Array
(
[department_id] => No Department
[cash_drawer] => 21112
[cash_drawer_sale_total] => 50.00
)
[8] => Array
(
[department_id] => No Department
[cash_drawer] => No Cash Drawer
[cash_drawer_sale_total] => 125.00
)
)
cashdrawer_refund
Array
(
[0] => Array
(
[department_id] => 50502
[cash_drawer] => 21112
[cash_drawer_refund_total] => 103.00
)
[1] => Array
(
[department_id] => No Department
[cash_drawer] => No Cash Drawer
[cash_drawer_refund_total] => 25.37
)
)
I feel like I'm really close to a solution but I can't get it right
When I run this code I'm getting two amounts for each original value. So (from what I can tell) I am meeting my conditional on multiple passes.
Array
(
[0] => Array
(
[department_id] => 80000
[cash_drawer] => 21112
[cash_drawer_total] => 64.00
)
[1] => Array
(
[department_id] => 80000
[cash_drawer] => 21112
[cash_drawer_total] => 64.00
)
[2] => Array
(
[department_id] => 80000
[cash_drawer] => 21117
[cash_drawer_total] => 15.00
)
[3] => Array
(
[department_id] => 80000
[cash_drawer] => 21117
[cash_drawer_total] => 15.00
)
[4] => Array
(
[department_id] => 80000
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 50.00
)
[5] => Array
(
[department_id] => 80000
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 50.00
)
[6] => Array
(
[department_id] => 50502
[cash_drawer] => 21112
[cash_drawer_total] => 90
)
[7] => Array
(
[department_id] => 50502
[cash_drawer] => 21112
[cash_drawer_total] => 193.00
)
[8] => Array
(
[department_id] => 50502
[cash_drawer] => 21113
[cash_drawer_total] => 30.00
)
[9] => Array
(
[department_id] => 50502
[cash_drawer] => 21113
[cash_drawer_total] => 30.00
)
[10] => Array
(
[department_id] => 50502
[cash_drawer] => 21117
[cash_drawer_total] => 10.00
)
[11] => Array
(
[department_id] => 50502
[cash_drawer] => 21117
[cash_drawer_total] => 10.00
)
[12] => Array
(
[department_id] => 50502
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 80.00
)
[13] => Array
(
[department_id] => 50502
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 80.00
)
[14] => Array
(
[department_id] => No Department
[cash_drawer] => 21112
[cash_drawer_total] => 50.00
)
[15] => Array
(
[department_id] => No Department
[cash_drawer] => 21112
[cash_drawer_total] => 50.00
)
[16] => Array
(
[department_id] => No Department
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 125.00
)
[17] => Array
(
[department_id] => No Department
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 99.63
)
)
I'm assuming this is happening because I'm iterating through each sale twice to analyze the refund. However, I'm not sure what to do about this.
This is my ultimate goal:
Array
(
[0] => Array
(
[department_id] => 80000
[cash_drawer] => 21112
[cash_drawer_total] => 64.00
)
[1] => Array
(
[department_id] => 80000
[cash_drawer] => 21117
[cash_drawer_total] => 15.00
)
[2] => Array
(
[department_id] => 80000
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 50.00
)
[3] => Array
(
[department_id] => 50502
[cash_drawer] => 21112
[cash_drawer_total] => 90
)
[4] => Array
(
[department_id] => 50502
[cash_drawer] => 21113
[cash_drawer_total] => 30.00
)
[5] => Array
(
[department_id] => 50502
[cash_drawer] => 21117
[cash_drawer_total] => 10.00
)
[6] => Array
(
[department_id] => 50502
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 80.00
)
[7] => Array
(
[department_id] => No Department
[cash_drawer] => 21112
[cash_drawer_total] => 50.00
)
[8] => Array
(
[department_id] => No Department
[cash_drawer] => No Cash Drawer
[cash_drawer_total] => 99.63
)
)
Any suggestions?