首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >多维表(超出工作表)为php数组。或者其他的想法?

多维表(超出工作表)为php数组。或者其他的想法?
EN

Stack Overflow用户
提问于 2016-05-19 21:38:36
回答 1查看 34关注 0票数 2

我有一张表,表示旅游规划系统的价格计算。

您可以在这里下载该表格:https://www.dropbox.com/s/ctam8iwym9pumjz/Example.xlsx?dl=0

现在,我想构建一个PHP函数,如:

priceCalc($kilometers,$numberOfPersons,$doubleTour)

参数的示例值:

  • $kilometers = 130.0; (双)
  • $numberOfPersons = 4; (整数)
  • $doubleTour = true; (布尔型)

结果必须具有以下值:= 218.00欧元(见超差表)

您将如何实现它最简单的方式(没有第三方类或扩展纯PHP)?

EN

回答 1

Stack Overflow用户

发布于 2016-05-23 16:30:20

明白了!

代码语言:javascript
运行
复制
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;
}

函数调用:

代码语言:javascript
运行
复制
echo getEcoPrice(200.1, 2, false);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37334459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档