如何取消或删除$ _SESSION中的一些嵌套数组

时间:2013-05-17 12:07:44

标签: php arrays session array-unset

我在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 个答案:

答案 0 :(得分:0)

$shop = 4;

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

答案 1 :(得分: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_filter建议使用deceze。

取消设置密钥的代码:

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

答案 2 :(得分: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();

答案 3 :(得分: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重命名为更有意义的东西。我不知道他们代表什么。