2016-09-28 100 views
0

我正試圖在數組內找到一個值。計算兩個數組之間的值

我的功能:

$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% 

應用的折扣如何使一個數組?

+0

是$ discount_quantity一個像[0] => string(1)「1」[1] => string(1)「5」[2] => string(2)「10」的數組? –

+0

是的,如果我理解你的問題 – kurama

回答

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

?>; 

輸出:Array([0] => 1 [1] => 4.5 [2] => 8.5);