某算法

<?php
function ExplodeLines($text, $columnNames)
{
$arr = array();
$r1 = explode("\n", $text);
unset($r1[0]);
unset($r1[count($r1)]);
foreach ($r1 as $v1)
{
$v1 = explode(',', $v1);
array_push($arr, $v1);
}
foreach ($arr as &$v)
{
$v = array_combine($columnNames, $v);
}
return $arr;

}


$text = "
Apple,20,red
Pear,10,yellow
";
$columnNames = array('Fruit', 'Number', 'Color');


$result = ExplodeLines($text, $columnNames);


var_export($result);


/*
array(
array('Fruit'=>'Apple', 'Number'=>'20', 'Color'=>'red'),
array('Fruit'=>'Pear', 'Number'=>'10', 'Color'=>'yellow'),
)
*/


含折扣的退货算法

function CalcRefundAmount($orderItems, $discountAmount, $refundItems)
{
	$arr = $arrAmout = array();
	foreach ($orderItems as $k=>$v)
	{
		$num = array_product($v);
		array_push($arr, $num);
	}
	$count = array_sum($arr);
	$realpay = $count - $discountAmount;
	foreach ($refundItems as $k=>$v)
	{
		$refundAmout = ($orderItems[$k]['price'] * $v) * ($realpay / $count);
		array_push($arrAmout, round($refundAmout, 2));
	}
		
	return array_sum($arrAmout);
}

//(5.0 * 1) * (20.0/30.0) = 3.33 元(精确到2位小数)
$orderItems = array(
	'ItemA'=>array('price'=>5.0, 'quantity'=>2),
	'ItemB'=>array('price'=>20.0, 'quantity'=>1),
);
$refundItems = array('ItemA'=>1);
$discountAmount=10;

$result = CalcRefundAmount($orderItems, $discountAmount, $refundItems);
echo $result;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值