多维表(excelsheet)作为php数组。还是其他任何想法?

时间:2016-05-19 21:38:36

标签: php arrays multidimensional-array phpexcel

我有一张excelsheet,其中的表格代表了旅游计划系统的价格计算。

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

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

priceCalc($公里,$ numberOfPersons,$ doubleTour)

参数的示例值:

  • $ km = 130.0; (双倍)
  • $ numberOfPersons = 4; (整数)
  • $ doubleTour = true; (布尔值)

结果必须使用这些值 = 218.00€ (请参阅excelsheet)

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

1 个答案:

答案 0 :(得分:0)

知道了!

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);