1

我有一个 $cart 数组(),它是发布到数据库之前的会话变量。

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”来表示具有特殊描述样式1的项目id“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 => 
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_quantity并增加/减少它们:special_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 回答