0

我正在使用 codeigniter 购物车库,但现在客户希望用户一次只能从一个类别中结帐,因为他们的支付网关存在一些问题。当前,该站点对所有类别都有一个单一的结帐逻辑。当用户将商品添加到购物车时,我有一个这样的数组

Array
(
    [d8df18561040f3d9bd9868f5c5aaa7c2] => Array
        (
            [rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
            [id] => MYU_SC1
            [qty] => 1
            [price] => 500
            [name] => WAEC Scratch Card
            [service_image] => assets/img/waec.jpg
            [service_category] => scratch_cards
            [subtotal] => 500
        )

    [99483fe03da62c9e98ce71232998f447] => Array
        (
            [rowid] => 99483fe03da62c9e98ce71232998f447
            [options] => Array
                (
                    [size] => 36
                    [colour] => N/A
                )

            [id] => 80433426a546064bf5f8d09a6e7fdabc
            [qty] => 1
            [price] => 5000
            [name] => Green Vee Jeans
            [service_image] => http://localhost/myunivacity/uploads/apparels/IMG_0425.JPG
            [service_category] => apparels
            [subtotal] => 5000
        )

)

我如何检查购物车中的商品是否具有相同的“service_category”元素值?谢谢您的帮助

4

1 回答 1

0

你可以用这样的东西:

<?php 

$categories = array();

foreach($this->cart->contents() as $cart_item) {
    if(!isSet($categories[$cart_item["service_category"]]) {
        $categories[$cart_item["service_category"]] = 1;        
    }
    else {
        $categories[$cart_item["service_category"]]++;
    }
}

print_r($categories);

?>

这用每个类别的计数填充数组

于 2012-11-15T10:45:44.507 回答