数组加减问题
已知数组a和b。
//数组a:
array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2X15',
'part_count' => '32',
),
1 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2Z25',
'part_count' => '32',
),
)
//数组b:
array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'total' => '48',
),
)
//数组a:
array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2X15',
'part_count' => '32',
),
1 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2Z25',
'part_count' => '16',
),
)
//数组a:
array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2X15',
'part_count' => '0',
),
1 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2Z25',
'part_count' => '16',
),
)
//数组a:
$a = array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2X15',
'part_count' => '32',
),
1 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2Z25',
'part_count' => '32',
),
);
//数组b:
$b = array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'total' => '48',
),
);
foreach($b as $source) {
$num = $source['total'];
foreach($a as $i=>$dest) {
if($num == 0) break;
if($dest['cust_no'] != $source['cust_no']) continue;
if($num >= $dest['part_count']) {
$num -= $dest['part_count'];
$res[] = $dest;
$a[$i]['part_count'] = 0;
}else {
$dest['part_count'] = $num;
$res[] = $dest;
$a[$i]['part_count'] -= $num;
$num = 0;
}
}
}
var_export($res);
var_export($a);
array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2X15',
'part_count' => '32',
),
1 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2Z25',
'part_count' => 16,
),
)
array (
0 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2X15',
'part_count' => 0,
),
1 =>
array (
'cust_no' => '310F6 1VA5A',
'lotno' => '2Z25',
'part_count' => 16,
),
)