根据距离获得价格

时间:2018-05-28 06:55:44

标签: php

我正在尝试按距离创建动态价格计算。我的要求与此类似 所以我需要一个能够通过的功能 距离,然后按距离获得价格

From 0 -10km = Rs10rs/km (fixed price)
10-15km = rs10Rs/km 
15 -20km = 9rs pre/km
20 -30km = 8.5rs per/km
25 -30km = 8rs per/km
30 -40km = 7.5rs per/km
30km -50km above = 7rs / km

foreach ($usersInfo as $index=> $users)
{
     $km= 0;
      $ridefrom=$users['ride_from'];
      $rideto=$users['ride_to'];//this is from lat long
      $tempLatLong = explode(',',$users['ride_from']);
      $tempLatLong1 = explode(',',$users['ride_to']);
      $key = array('lat','long');
      $to = array_combine($key,$tempLatLong); 
      $from = array_combine($key,$tempLatLong1);
      $km=distanceCalculation($from['lat'],$from['long'],$to['lat'],$to['long']).'km';
      $usersInfo[$index]['distance'] =$km;
      $carprice=10;// price will be change dynamical from back end admin can change this.

      if($carprice){
      $price=$km*10;
      }
      else{
        $price=$km*9;  
      }
      $usersInfo[$index]['price'] =$price;


                    return $usersInfo;
    }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

function price_per_km($distance, $mode) {
  if ($mode == 'car') {
    switch (true) {
      case $distance <= 10:
        return 10*$distance;
      case $distance <= 15:
        return 10*$distance;
      case $distance <= 20:
        return 9*$distance;
      case $distance <= 25:
        return 8.5*$distance;
      case $distance <= 30:
        return 8*$distance;
      case $distance <= 40:
        return 7.5*$distance;
      default:
        return 7*$distance;
    }
  }
  else {
    // bike
    switch (true) {
      case $distance <= 10:
        return 12*$distance;
      case $distance <= 15:
        return 11*$distance;
      case $distance <= 20:
        return 10*$distance;
      case $distance <= 25:
        return 9*$distance;
      default:
        return 8*$distance;
    }
  }
}

echo price_per_km(5, 'car') . "\n";
echo price_per_km(12, 'car') . "\n";
echo price_per_km(25, 'car') . "\n";
echo price_per_km(35, 'car') . "\n";
echo price_per_km(5, 'bike') . "\n";
echo price_per_km(15, 'bike') . "\n";
echo price_per_km(35, 'bike') . "\n";

输出:

50
120
212.5
227.5
60
165
270