1

$cart array() があります。これは、データベースに投稿する前のセッション変数です。

array (size=2)
  1 => 
    array (size=5)
      'id' => string '1' (length=1)
      'price' => string '10' (length=2)
      'qty' => int 2
      'item_desc' => string 'Fast Food 01' (length=12)
      'special_desc' => string '' (length=0)
  '1S' => 
    array (size=5)
      'id' => string '1' (length=2)
      'price' => string '10' (length=2)
      'qty' => string '1' (length=1)
      'item_desc' => string 'Fast Food 01' (length=12)
      'special_desc' => string 'Special Cook style 1' (length=3)

例としてitem idは「1」ですが、重複キーの競合を避けるため「S」を付けて特別な記述をした配列要素であることを示しています。

これは私の現在のコーディングです

...
if (array_key_exists($item_id, $cart)) {
  $key_item_id = $item_id . 'S';
}

$cart[$key_item_id] = array('id' => $item_id, 'price' => $price, 'qty' => $qty, 'item_desc' => $item_desc, 'special_desc' => $special_desc);
...

ただし、特別な説明が複数ある可能性があるため、「S」の後にカウンター番号を付ける必要があると思います。たとえば、「1S1」はアイテムID「1」を特別な説明スタイル1、「1S2」で表します。特別な説明スタイル 2 など。

array (size=2)
  1 => 
    array (size=5)
      'id' => string '1' (length=1)
      'price' => string '10' (length=2)
      'qty' => int 2
      'item_desc' => string 'Fast Food 01' (length=12)
      'special_desc' => string '' (length=0)
  '1S1' => 
    array (size=5)
      'id' => string '1' (length=2)
      'price' => string '10' (length=2)
      'qty' => string '1' (length=1)
      'item_desc' => string 'Fast Food 01' (length=12)
      'special_desc' => string 'Special Cook style 1' (length=3)

  '1S2' => 
    array (size=5)
      'id' => string '1' (length=2)
      'price' => string '10' (length=2)
      'qty' => string '1' (length=1)
      'item_desc' => string 'Fast Food 01' (length=12)
      'special_desc' => string 'Special Cook style 2' (length=3)

私の質問は、現在のカウンターを実現できるように、$cart のアイテム ID「1」の数を特別な説明で「カウント」する方法です。

ありがとう。

4

3 に答える 3

1

キーを使用するのは良い考えではありません。same ID配列アイテムのすべてのアイテムを1つのキーで格納する必要があると思います。

1 => 
array (size=4)
  'price' => string '10' (length=2)
  'qty' => int 2
  'item_desc' => string 'Fast Food 01' (length=12)
  'special_desc' => string '' (length=0)
array (size=4)
  'price' => string '10' (length=2)
  'qty' => string '1' (length=1)
  'item_desc' => string 'Fast Food 01' (length=12)
  'special_desc' => string 'Special Cook style 1' (length=3)
array (size=4)
  'price' => string '10' (length=2)
  'qty' => string '1' (length=1)
  'item_desc' => string 'Fast Food 01' (length=12)
  'special_desc' => string 'Special Cook style 2' (length=3)`

さらに、配列に新しいアイテムを追加するときに、キーを追加したり、キーtotal_quantityspecial_items_quantity増減したりできます。

1 => 
total_quantity => 4    // now it is 4
special_items_quantity => 2    // now it is 2
array (size=4)
  'price' => string '10' (length=2)
  'qty' => int 2
  'item_desc' => string 'Fast Food 01' (length=12)
  'special_desc' => string '' (length=0)
array (size=4)
  'price' => string '10' (length=2)
  'qty' => string '1' (length=1)
  'item_desc' => string 'Fast Food 01' (length=12)
  'special_desc' => string 'Special Cook style 1' (length=3)
array (size=4)
  'price' => string '10' (length=2)
  'qty' => string '1' (length=1)
  'item_desc' => string 'Fast Food 01' (length=12)
  'special_desc' => string 'Special Cook style 2' (length=3)`
于 2013-08-04T10:56:24.207 に答える
1

の値を$index製品 ID に設定する$cntと、その製品の通常注文と特別注文の合計数量が取得されます。

$index = 1;
$cnt = array('total' => 0, 'normal' => 0,  'special' => 0);
array_walk($arr, function ($i, $k) use(&$cnt, $index){
    if ($i['id'] == $index){
        $cnt['total'] += $i['qty'];
        if ((string)$k == (string)$index) $cnt['normal'] += $i['qty'];
        else $cnt['special'] += $i['qty'];
    }
});
print_r($cnt);

注: このソリューションには、PHP 5.3 以降が必要です。

于 2013-08-04T11:49:12.363 に答える
-1

うわー、その複雑さは無駄です....他の方法を使用する必要があります...

また、次のようなことを試すこともできます。

$count = 0;

foreach($cart as $item){

    if(!empty($item['special_desc'])) $count++;
}

// count is the number of special_desc in the $cart array...
于 2013-08-04T10:51:53.450 に答える