PHP如何编写带有特殊说明的购物车代码


PHP How to code shopping cart with special description?

我有一个$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)

举个例子,项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",用特殊描述样式2表示"1S2",依此类推

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)

我的问题是如何"计数"有多少物品id为"1"的$cart,以便我可以实现它的当前计数器?

谢谢。

使用键不是个好主意。我认为应该用一个键将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_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)`

通过将$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或更高版本。

哇,那复杂的东西什么都没有。。。。你应该用另一种方法。。。

另外,你也可以试试这样的东西:

$count = 0;
foreach($cart as $item){
    if(!empty($item['special_desc'])) $count++;
}
// count is the number of special_desc in the $cart array...