2016-09-28 11 views
0

配列内の値を検索しようとしています。2つの配列間の値を計算する

私の機能:

$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] =>文字列(1) "1" [1] =>文字列(1) "5" [2] =>文字列(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); 

?>; 

出力:配列([0] => 1 [1] => 4.5 [2] => 8.5)。

関連する問題