1

私はcodeigniterショッピングカートを使用していますが、その「カート」配列は次のとおりです。

Array (
[a87ff679a2f3e71d9181a67b7542122c] => Array
    (
        [rowid] => a87ff679a2f3e71d9181a67b7542122c
        [id] => 4
        [qty] => 1
        [price] => 12.95
        [name] => Maroon Choir Stole
        [image] => 2353463627maroon_3.jpg
        [custprod] => 0
        [subtotal] => 12.95
    )

[8f14e45fceea167a5a36dedd4bea2543] => Array
    (
        [rowid] => 8f14e45fceea167a5a36dedd4bea2543
        [id] => 7
        [qty] => 1
        [price] => 12.95
        [name] => Shiny Red Choir Stole
        [image] => 2899638984red_vstole_1.jpg
        [custprod] => 0
        [subtotal] => 12.95
    )

[eccbc87e4b5ce2fe28308fd9f2a7baf3] => Array
    (
        [rowid] => eccbc87e4b5ce2fe28308fd9f2a7baf3
        [id] => 3
        [qty] => 1
        [price] => 14.95
        [name] => Royal Blue Choir Stole
        [image] => 1270984005royal_vstole.jpg
        [custprod] => 1
        [subtotal] => 14.95
    )

)。

私の目標は、この多次元配列を何らかの方法でループすることです。キーと値のペアが「custprod == 1」の商品が存在する場合、チェックアウトページに1つの商品が表示され、カートにカスタム商品がない場合は別の商品が表示されます。 。どんな助けでも大歓迎です。ありがとう。

4

3 に答える 3

2

ループするのではなく、 をcustprod使用してキーを確認できますarray_key_exists。または、単純に確認してくださいarr['custprod'] isset(両方の関数の処理がnull異なります)。

$key = "custprod";
$arr = Array(
    "custprod" => 1, 
    "someprop" => 23
);
if (array_key_exists($key, $arr) && 1 == $arr[$key]) {
    // 'custprod' exists and is 1
}
于 2012-11-24T22:39:33.987 に答える
1
function item_exists($cart, $custprod) {
    foreach($cart as $item) {
        if(array_key_exists("custprod", $item) && $item["custprod"] == $custprod) {
            return true;
        }
    }
    return false;
}

これで、この関数を使用して製品がスタックに存在するかどうかを確認できます。

if(item_exists($cart, 1)) {
    // true
} else {
   // false
}
于 2012-11-24T22:59:59.507 に答える
0

それをチェックするには、配列をループする必要があります。

$cust_prod_found = false;

foreach($this->cart->contents() as $item){
    if (array_key_exists("custprod", $item) && 1 == $item["custprod"]) {
        $cust_prod_found = true; break;
    }
}

if ($cust_prod_found) {
    // display one thing
} else {
    // display another thing
}
于 2012-11-24T22:57:08.813 に答える