【发布时间】:2016-07-08 10:23:04
【问题描述】:
如果两个键相互匹配,我正在尝试合并两个数组并在数组中减去一个值。
这是我迄今为止编写的代码。
$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
)
)
我觉得我真的很接近解决方案,但我无法正确解决
当我运行此代码时,每个原始值都会得到两个金额。所以(据我所知)我正在满足我多次通过的条件。
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 个元素。
-
是的,我正在尝试按 department_id 为每个现金抽屉执行此操作(cash_drawer 不是唯一的,deptartment_id 是)。
-
这个想法是销售总额可能没有退款,但如果确实有退款,我需要从相应的总额中减去它并创建我的新数组。
标签: php arrays for-loop multidimensional-array