如何取消设置或删除一些嵌套数组在$_SESSION



我在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的项目?

使用一个简单的循环和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重命名为更有意义的名称。我不知道它们代表什么。

$shop = 4;
$_SESSION['customer']['basket'] = array_filter(
    $_SESSION['customer']['basket'],
    function (array $item) use ($shop) { return $item['shopid'] != $shop; }
);

您可能需要这样做:

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();

您可以递归地搜索您的篮子并获得具有特定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_filter,就像deze建议的那样。

取消键设置的代码:

$keys = array_search_recursive('4', $_SESSION['customer']['basket']);
if(!empty($keys)) {
    foreach($keys as $item) {
        unset($_SESSION['customer']['basket'][$item[0]]);
    }
}

相关内容

  • 没有找到相关文章

最新更新