计算两个数组之间的值

时间:2016-09-28 02:38:17

标签: php arrays

我试图在数组中找到一个值。

我的职能:

$discount_quantity = $Products->getProductsDiscountQuantity($products_id)返回此信息:

[0] => string(1) "1" [1] => string(1) "5" [2] => string(2) "10" 

我有一个数量,例如$qty = 6,我需要在那个

上应用折扣
if $qty < 5 then $discount = 0%
if $qty > 5 et qty < 10 then $discount = 10%
if $qty > 10 then $discount = 15%

如何使用数组?

1 个答案:

答案 0 :(得分:1)

<?php
$discount_quantity = array( "1", "5", "10");

foreach($discount_quantity as $k => $v)
{
  if($v < 5)
  {
    $v = $v;
  }
  elseif($v >= 5 && $v < 10)
  {
    $v = $v - ($v * 10 / 100);
  }
  elseif($v >= 10)
  {
    $v = $v - ($v * 15 / 100);
  }
  $discount_quantity[$k] = $v;
}

print_r($discount_quantity);

?>;

输出:数组([0] =&gt; 1 [1] =&gt; 4.5 [2] =&gt; 8.5);