0

我的购物篮中有一些物品SESSION

每件商品可能用于一家商店,另一件商品可能用于另一家商店。

我想要当客户点击购物篮中每个商店项目列表的创建因素按钮时。如何删除或取消设置我的购物篮中的某些商品,这些商品shopid与客户点击此商店的创建因子相同shopid

例如,我的会话数组是:

Array('customer' => Array('basket' => Array(
                    '9_2' => Array
                        (
                            "row" => "0",
                            'item' => 'cd',
                            'count' => '1',
                            'sale_start_date' => '1391-12-25 19:27:56',
                            'sale_end_date' => '1392-04-20 19:27:49',
                            'sale_price' => '40500',
                            'price' => '54564',
                            'id' => '999035',
                            'shopid' => '4'
                        ),
                        '999_17' => Array
                        (
                            'row' => '1',
                            'item' => 'car',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '6'
                        ),
                        '9_3' => Array
                        (
                            'row' => '2',
                            'item' => 'book',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '4'
                        ),
                        '10_5' => Array
                        (
                            'row' => '2',
                            'item' => 'dvd',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '5'
                        )
                    )
                )
            );

你可以看到一些项目是不同shopid的,它不是排序的。

例如,如何shopid=4从我的购物篮中删除那些物品?

4

4 回答 4

0
$shop = 4;

$_SESSION['customer']['basket'] = array_filter(
    $_SESSION['customer']['basket'],
    function (array $item) use ($shop) { return $item['shopid'] != $shop; }
);
于 2013-05-17T12:14:24.050 回答
0

您可以递归搜索您的购物篮并获取所有具有特定shop_id. 例如这个函数:

function array_search_recursive($needle, $haystack, $key = false, $path = null) { 
    if(empty($path['level'])) $path['level'] = 0; 
    if(empty($path['work'])) $path['work'] = array(); 
    if(!is_array($haystack)) $haystack = array(); 

    foreach($haystack as $_key => $_value) { 
        if(is_scalar($_value) && $_value == $needle && !$key) { 
            $path['work'][$path['level']] = $_key; 
            $path['found'][] = $path['work']; 
        } elseif(is_scalar($_value) && $_value == $needle && $_key == $key) { 
            $path['work'][$path['level']] = $_key; 
            $path['found'][] = $path['work']; 
        } elseif (is_array($_value)) { 
            $path['work'][$path['level']] = $_key; 
            $path['level'] += 1; 
            $path = array_search_recursive($needle, $_value, $key, $path); 
        } 
    } 

    array_splice($path['work'], $path['level']); 
    $path['level'] -= 1; 

    if($path['level'] == '-1') {
        return $path['found'];
    } else return $path;

}

会在调用array_search_recursive('4', $_SESSION['customer']['basket'])返回

Array
(
    [0] => Array
        (
            [0] => 9_2
            [1] => shopid
        )

    [1] => Array
        (
            [0] => 9_3
            [1] => shopid
        )

)

然后你可以循环你的篮子并删除这些键。还有其他方法可以做到这一点,例如建议使用array_filterlike deceze。

取消设置键的代码:

$keys = array_search_recursive('4', $_SESSION['customer']['basket']);
if(!empty($keys)) {
    foreach($keys as $item) {
        unset($_SESSION['customer']['basket'][$item[0]]);
    }
}
于 2013-05-17T12:18:59.990 回答
0

你可能需要做这样的事情:

function selectUniqueByAttribute($array, $attributeName)
{
    $keysArray = array();
    foreach($array as $key=>$value){
        $keysArray[$value[$attributeName]] = $key;
    }

    $output = array();
    foreach($keysArray as $key){
        $output[$key] = $array[$key];
    }

    return $output;
}
$data = array('customer' =>  array(
                'basket' => array(
                        '9_2' => array
                            (
                                "row" => "0",
                                'item' => 'cd',
                                'count' => '1',
                                'sale_start_date' => '1391-12-25 19:27:56',
                                'sale_end_date' => '1392-04-20 19:27:49',
                                'sale_price' => '40500',
                                'price' => '54564',
                                'id' => '999035',
                                'shopid' => '4'
                            ),
                            '999_17' => array
                            (
                                'row' => '1',
                                'item' => 'car',
                                'count' => '1',
                                'sale_start_date' => '0000-00-00 00:00:00',
                                'sale_end_date' => '0000-00-00 00:00:00',
                                'sale_price' => '0',
                                'price' => '520000',
                                'id' => '999039',
                                'code' => 'b125nh',
                                'shopid' => '6'
                            ),
                            '9_3' => array
                            (
                                'row' => '2',
                                'item' => 'book',
                                'count' => '1',
                                'sale_start_date' => '0000-00-00 00:00:00',
                                'sale_end_date' => '0000-00-00 00:00:00',
                                'sale_price' => '0',
                                'price' => '520000',
                                'id' => '999039',
                                'code' => 'b125nh',
                                'shopid' => '4'
                            ),
                            '10_5' => array
                            (
                                'row' => '2',
                                'item' => 'dvd',
                                'count' => '1',
                                'sale_start_date' => '0000-00-00 00:00:00',
                                'sale_end_date' => '0000-00-00 00:00:00',
                                'sale_price' => '0',
                                'price' => '520000',
                                'id' => '999039',
                                'code' => 'b125nh',
                                'shopid' => '5'
                            )
                        )
                    )
                );

$filteredArray = array(
                    'customer'  =>  array(
                                            'basket'    =>  selectUniqueByAttribute($data['customer']['basket'], 'shopid')
                                         )
                  );
print_r('<pre>');
print_r($filteredArray);
die();
于 2013-05-17T12:29:21.473 回答
0

使用一个简单的循环并内置 PHP 数组函数:

$deleteShopId = 4;
$test = Array('customer' => Array('basket' => Array(
                    '9_2' => Array
                        (
                            "row" => "0",
                            'item' => 'cd',
                            'count' => '1',
                            'sale_start_date' => '1391-12-25 19:27:56',
                            'sale_end_date' => '1392-04-20 19:27:49',
                            'sale_price' => '40500',
                            'price' => '54564',
                            'id' => '999035',
                            'shopid' => '4'
                        ),
                        '999_17' => Array
                        (
                            'row' => '1',
                            'item' => 'car',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '6'
                        ),
                        '9_3' => Array
                        (
                            'row' => '2',
                            'item' => 'book',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '4'
                        ),
                        '10_5' => Array
                        (
                            'row' => '2',
                            'item' => 'dvd',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '5'
                        )
                    )
                )
            );

foreach($test['customer']['basket'] as $something => $somethingElse){
$temp = array_search($deleteShopId,$somethingElse);
   if($temp !== FALSE && $temp =='shopid'){
      unset($test['customer']['basket'][$something]);
   }
}

var_dump($test['customer']['basket']);

输出:

array(2) {
  ["999_17"]=>
  array(10) {
    ["row"]=>
    string(1) "1"
    ["item"]=>
    string(3) "car"
    ["count"]=>
    string(1) "1"
    ["sale_start_date"]=>
    string(19) "0000-00-00 00:00:00"
    ["sale_end_date"]=>
    string(19) "0000-00-00 00:00:00"
    ["sale_price"]=>
    string(1) "0"
    ["price"]=>
    string(6) "520000"
    ["id"]=>
    string(6) "999039"
    ["code"]=>
    string(6) "b125nh"
    ["shopid"]=>
    string(1) "6"
  }
  ["10_5"]=>
  array(10) {
    ["row"]=>
    string(1) "2"
    ["item"]=>
    string(3) "dvd"
    ["count"]=>
    string(1) "1"
    ["sale_start_date"]=>
    string(19) "0000-00-00 00:00:00"
    ["sale_end_date"]=>
    string(19) "0000-00-00 00:00:00"
    ["sale_price"]=>
    string(1) "0"
    ["price"]=>
    string(6) "520000"
    ["id"]=>
    string(6) "999039"
    ["code"]=>
    string(6) "b125nh"
    ["shopid"]=>
    string(1) "5"
  }
}

当然,将 $something 和 $somethingElse 重命名为更有意义的名称。我不知道它们代表什么。

于 2013-05-17T12:36:58.947 回答