2016-07-20 11 views
0

次の配列から、itemTypeがZoであるという条件で数量と価格を倍にしようとしています。他の値の値に基づいて2つの配列の値を掛けてください

(
    [customer] => 4 
    [itemNo] => Array 
     (
      [0] => 1 
      [1] => 2 
     ) 

    [itemName] => Array 
     (
      [0] => Type A 
      [1] => Type B 
     ) 

    [itemType] => Array 
     (
      [0] => Zo 
      [1] => Ram 
     ) 

    [quantity] => Array 
     (
      [0] => 2 
      [1] => 3 
     ) 

    [price] => Array 
     (
      [0] => 500 
      [1] => 2000 
     ) 
) 

これまで私がこれまでに試したことはありますが、成功しませんでした。

$lq = 0; 
$total =0; 

    for ($i=0;$i<count($_REQUEST['price']);$i++) { 
     if(in_array("Ram", $_REQUEST['itemType'])){ 
       $total += $_REQUEST['price'][$i] * $_REQUEST['quantity'][$i]; 
     }else{ 
       $lq += $_REQUEST['quantity'][$i]; 
     } 
    } 

echo ($total).'<br>'; 
echo ($lq); 

私の予想される出力は次のようになります。

$total = 1000;//Quantity x Price 
$lq = 3//Quantity only 
+0

これはちょうど、各反復で、 '=='使用し、 '常に' in_array( "RAM"、$ _REQUEST [ 'itemTypeには'])満足させます代わりに、答えは – Ghost

+0

@bmaのように、あなたのデータは 'Zo'型の複数の項目を持つことができますか?または入力に' Zo'が1つしかありませんか? – BeetleJuice

+0

@BeetleJuice '$ _REQUEST'にはフォーム入力が含まれているので、おそらくユーザーはそのタイプを選択しています。 – Barmar

答えて

1

あなたは合計に追加している同じ項目のitemTypeをチェックしていません。あなたはのいずれかのにはitemTypeがあるかどうかだけチェックしています。 Zoではなく、Ramも探しています。

if ($_REQUEST['itemType'][$i] == 'Zo') { 
    $total += $_REQUEST['price'][$i] * $_REQUEST['quantity'][$i]; 
} else { 
    $lq += $_REQUEST['quantity'][$i]; 
} 
+0

@bmapachuauで正しい出力が得られるhttps://3v4l.org/LTuYo – Ghost

0

試してみてください。

$data = $_REQUEST; 
$key = array_search('Zo',$data['itemType']); 
$total = $data['quantity'][$key] * $data['price'][$key];//Zo price*qty 
$lq = array_sum($data['quantity']) - $data['quantity'][$key];//non-Zo qty 

Live demo

+0

複数の 'Zo'アイテムがある場合、これは機能しません。 – Barmar

+0

True;私はOPにその入力が可能かどうか質問します – BeetleJuice

+0

'$ lq'は' itemType = Zo'を持たないすべてのアイテムの合計数を含みます – Barmar

関連する問題