我試圖組合兩個數組並在數組中減去一個值,如果兩個鍵彼此匹配的話。如果兩個值匹配,組合/修改PHP數組
這是我迄今爲止編寫的代碼。
$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);
下面是我想要操作的$ cashdrawer_sales和$ cashdrawer_refund數組的示例。
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
)
)
我覺得我真的很接近的解決方案,但我不能得到正確
當我運行這段代碼我m爲每個原始價值獲得兩個金額。所以(從我所知道的情況來看)我在多次通過時滿足了我的條件。
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
)
)
我假設發生這種情況是因爲我通過兩次銷售迭代來分析退款。但是,我不知道該怎麼做。
這是我的終極目標是:
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
)
)
有什麼建議?
是否要爲每個cash_drawer單獨銷售和退款? –
我想從相應的銷售總額中扣除退款總額,然後創建新總計的新數組。 –
好的,但你想爲每個cash_drawer做這項工作嗎?由於您的銷售陣列有7個元素,您的退款陣列有2個元素。 –