2012-04-25 10 views
0

次のコードがあります。私はelseifの部分に$pIdが既に配列に入っているかどうかを確認したい場合は、配列に新しい$pIdを追加するのではなく、quantitypriceを増やしたいのですが。配列に値が存在するかどうかを確認し、配列に値があるかどうかを確認します。

私は私が間違った方法を使用していますか、私の配列構造が間違っているが、私はそれがそれらの値を増やすことを得るカント場合、私は配列への余分な次元を追加しました

if (!isset($_SESSION['cart'])) { 
    $_SESSION['cart'] = array(); 
    $_SESSION['cart']['pid'][] = $pid; 
    $_SESSION['cart']['pid']['price'] = $price; 
    $_SESSION['cart']['pid']['quantity'] = $quantity; 
    $_SESSION['cart']['total_price'] = $price; 
    $_SESSION['cart']['total_items'] = $quantity; 
}elseif(array_key_exists($pid, $_SESSION['cart'])){ 
    //increase price 
    //increase quantity 
}else{ 
    $_SESSION['cart']['pid'][] = $pid; 
    $_SESSION['cart']['pid']['price'] = $price; 
    $_SESSION['cart']['total_price'] += $price; 
    $_SESSION['cart']['total_items'] += $quantity; 
} 
+0

何が増えますか? – PeeHaa

+0

@RepWhoringPeeHaa現在の値+新しい値。例えば ​​'price + ='; – code511788465541441

答えて

1

かどうかを知りませんので、それを簡単に選択することができます。

if (!isset($_SESSION['cart'])) { 
    $_SESSION['cart'] = array('pid' => array(), 'total_price' => 0, 'total_items' => 0); 
    $_SESSION['cart']['pid'][$pid] = array(); 
    $_SESSION['cart']['pid'][$pid]['price'] = $price; 
    $_SESSION['cart']['pid'][$pid]['quantity'] = $quantity; 
    $_SESSION['cart']['total_price'] = $price; 
    $_SESSION['cart']['total_items'] = $quantity; 
}elseif(array_key_exists($pid, $_SESSION['cart']['pid'])){ 
    $_SESSION['cart']['pid'][$pid]['price'] = 'new price'; 
    $_SESSION['cart']['pid'][$pid]['quantity'] = 'new quantity'; 
}else{ 
    $_SESSION['cart']['pid'][$pid]['price'] = $price; 
    $_SESSION['cart']['total_price'] += $price; 
    $_SESSION['cart']['total_items'] += $quantity; 
} 

私はpidが何の略かわからないが、一見それは本当に、記述は見えません。たぶんproductsはより良い鍵でしょうか?

+0

はとても感謝しています! – code511788465541441

関連する問題