簡體   English   中英

多維數組

[英]Multidimensional Arrays

我有一個功能,可以在購物車中添加自定義的客串。 購物車存儲在會話變量中。 如果客戶決定構建相同的客串,我不想在陣列中添加另一個條目,我只是希望能夠在所述產品的數量上再增加1個。 問題是,當腳本到達要檢查數組中是否存在值的位置時,腳本將返回false並將新條目添加到數組中。 我對php很陌生,所以我不確定是否要以正確的方式進行操作。

function AddToCart($subpid,$subtype,$subprice,$subtotal,$subcarving,$subline1,$subline2){
    global $form;

    $exist = false;
    $i=0;

    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array(0 => array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1));
    }
    else{
        foreach($_SESSION['cart'] as $item){
            $i++;
            while(list($key,$value) = each($item)){
                /* If product exist add 1 to quantity */
                if($key == "Product ID" && $value == $subpid && $key == "Type" && $value == $subtype && $key == "Price" && $value == "$".$subprice && $key == "Subtotal" && $value == "$".$subtotal  && $key == "Carving" && $value == $subcarving && $key == "Line 1" && $value == $subline1 && $key == "Line 2" && $value == $subline2){
                    array_splice($_SESSION['cart'], $i-1, 1, array(array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => $item['Quantity'] + 1)));
                    $exist = true;
                }
            }
        }
        if($exist == false){
            array_push($_SESSION['cart'], array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1));
        }
    }
    return 0;
}

如果我只使用:$ key ==“產品ID” && $ value == $ subid,它將返回true並更新數量,但是問題在於如果客戶購買了兩個具有相同id但雕刻不同的客串它或雕刻品我的購物車將被關閉。

我認為您正在使這種方式變得比必須的更加混亂。

我將這樣設置您的購物車數組:

$cart[0]["name"] = "whatever";
$cart[0]["ProductID"] = "1234";
$cart[0]["price"] = 0.00;
$cart[0]["quantity"] = 1;
$cart[0]["options"] = array(
     "subcarving" => "asdf",
     "subline1"   => "asdfsafd",
     "subline2"   => "asfdsadfdf");

然后,您可以像這樣簡單地循環處理它:

$didadd = 0;
for($x = 0; $x < sizeof($cart); $x++) {
    if($subid == $cart[$x]["ProductID"]) {
        // check options
        $sameOpts = 1;
        foreach($cart[$x]["options"] as $key => $val) {

            if($val != ${$key}) { // checks if the current items option[val] = function(val)
                $sameOpts = 0;
            }

        }

        if($sameOpts) {
            $didadd = 1; // sets the flag that we added the element.
            // increase quantity since the product id and options matched.

        } else {
            $didadd = 1;
            // add new element
            // sets the flag that we added the element
        }

    }
} 

if(!$didadd) { 
    // still need to add the item
    // do code to create new $cart item here.

}

這是行不通的,因為您同時將每個鍵與&&語句進行比較,但是一次循環遍歷每個鍵。 取出while循環,然后像這樣進行比較:

if( $item['Product ID'] == $subpid ... //etc ) {

}

另外,您不需要array_splice即可更新項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM