/**
* 运费拆分.
*
* @param int $f
* @param array $arr
*
* @return array
*/
protected function freightSplit($f = 0, $arr = []): array
{
$count = count($arr); //总记录数
$allNum = array_sum(array_column($arr, 'product_number'));
//排序
uksort($arr, function ($ak, $bk) use ($arr) {
$a = $arr[$ak];
$b = $arr[$bk];
if ($a['product_number'] === $b['product_number']) {
return $ak - $bk;
}
return $a['product_number'] > $b['product_number'] ? 1 : -1;
});
$i = 1;
$freightCumulative = 0;
foreach ($arr as &$val) {
if ($count == 1) {
$val['_freight_amount'] = $f;
} elseif ($i == $count) {
$val['_freight_amount'] = bcsub($f, $freightCumulative, 2);
} else {
$val['_freight_amount'] = bcmul(bcdiv($val['product_number'], $allNum, 2), $f, 2);
$freightCumulative = bcadd($freightCumulative, $val['_freight_amount'], 2);
}
++$i;
}
ksort($arr);
return $arr;
}
本文档使用 521Wiki 发布