根据条件重写php数组

时间:2013-06-27 02:42:28

标签: php arrays rewrite conditional-statements

我有一个阵列,我希望根据天数更改车辆价格是我的阵列想重写阵列,我得到$ rental_days的天数,并根据总价格乘以,现在我想要改变它,如果天数是1,2,3,4,如果5,6,7比最后价格适用将收取费用那么我应该如何基于这个条件重写数组(total_price)?

        $vehicles[$result['vehicle_id']] = array(
            'vehicle_id'    => $result['vehicle_id'],
            'title'         => $result['manufacturer']." ".$result['series'],
            'manufacturer'  => $result['manufacturer'],
            'series'        => $result['series'],
            'year'          => $result['year'],
            'class'         => $result['class'],
            'image'         => $image,
            'image_thumb'   => $image_thumb,
            'description'   => $description,
            'seats'         => $vehicle_meta['seats'],
            'doors'         => $vehicle_meta['doors'],
            'conditioning'  => $vehicle_meta['conditioning'],
            'transmission'  => $vehicle_meta['transmission'],
            'total_price'     => $rc_currency->format($rental_days*$result['rent']),
            'daily_price'     => $rc_currency->format($result['rent'])
        );

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话......

你的if语句要检查几天,然后将费用应用到底部的总数

不确定您是否要申请一次,或者按天数计算费用......我会留给您指出

      if(($rental_days>0)&&($rental_days<=4)){
       $charge = 12.00; //whatever you charge for 4 days
       } elseif(($rental_days>4)&&($rental_days<=7)){
       $charge = 25.00; //whatever you charge for 567 days
      } elseif($rental_days>7){
       $charge = 40.00; //whatever you charge for 8 days
      } else {$charge = 0;}

      $vehicles[$result['vehicle_id']] = array(
        'vehicle_id'    => $result['vehicle_id'],
        'title'         => $result['manufacturer']." ".$result['series'],
        'manufacturer'  => $result['manufacturer'],
        'series'        => $result['series'],
        'year'          => $result['year'],
        'class'         => $result['class'],
        'image'         => $image,
        'image_thumb'   => $image_thumb,
        'description'   => $description,
        'seats'         => $vehicle_meta['seats'],
        'doors'         => $vehicle_meta['doors'],
        'conditioning'  => $vehicle_meta['conditioning'],
        'transmission'  => $vehicle_meta['transmission'],
        'total_price'     => $rc_currency->format(($rental_days*$result['rent'])+$charge),
        'daily_price'     => $rc_currency->format($result['rent'])
    );