我有一张表,表示旅游规划系统的价格计算。
您可以在这里下载该表格:https://www.dropbox.com/s/ctam8iwym9pumjz/Example.xlsx?dl=0
现在,我想构建一个PHP函数,如:
priceCalc($kilometers,$numberOfPersons,$doubleTour)
参数的示例值:
结果必须具有以下值:= 218.00欧元(见超差表)
您将如何实现它最简单的方式(没有第三方类或扩展纯PHP)?
发布于 2016-05-23 16:30:20
明白了!
function getEcoPrice($distance, $persons, $doubleTour) {
$distance = ceil($distance); //round up to next integer
$startPrice = 69;
$endprice = $startPrice;
$amountPlusPerson = array(0, 0, 20, 20, 10, 10, 10, 10, 10); //first index is just for setting index = number of persons. value to add, if extra person.
$amountNextDistance = array(0, 10, 10, 10, 10, 10, 10, 10, 10); //first index is just for setting index = number of distance steps. value to add, if next distance is reached.
$amountDoubleTour = array(-20, -20, -20, -20, -20, -20, -20, -20, -20); //value to add, if doubleTour is enabled.
$index = 0;
switch (true) {
case $distance <= 130:
$index = 0;
break;
case $distance <= 160:
$index = 1;
break;
case $distance <= 170:
$index = 2;
break;
case $distance <= 180:
$index = 3;
break;
case $distance <= 190:
$index = 4;
break;
case $distance <= 200:
$index = 5;
break;
case $distance <= 210:
$index = 6;
break;
case $distance <= 220:
$index = 7;
break;
case $distance <= 230:
$index = 8;
break;
case $distance > 230:
return 99999;
break;
}
for($i = 0; $i <= $index; $i++) {
$endprice += $amountNextDistance[$i];
};
for ($i = 0; $i <= $persons; $i++) {
$endprice += $amountPlusPerson[$i];
}
if ($doubleTour) {
$endprice = $endprice * 2 + $amountDoubleTour[$index];
}
return $endprice;
}
函数调用:
echo getEcoPrice(200.1, 2, false);
https://stackoverflow.com/questions/37334459
复制相似问题