简体   繁体   English

使用PHP中的数组键删除Cookie

[英]Delete cookie using array key in PHP

Just wanna know how to delete cookies on PHP. 只想知道如何在PHP上删除Cookie。 below are printed result of $_COOKIES . 以下是$_COOKIES打印结果。 So, I have an array of cart which store information as it suggested, and on my cart.php file I got a button to delete an agenda or options. 因此,我有一个购物车阵列,可以按照建议的方式存储信息,在我的cart.php文件中,我有一个删除议程或选项的按钮。

Array
(
    [cart] => Array
        (
            [90406_cart] => Array
                (
                    [id_agenda] => 7
                    [qty_agenda] => 3
                    [options] => Array
                        (
                            [28962_option] => Array
                                (
                                    [id_option] => 3
                                    [qty_option] => 1
                                )
                            [52058_option] => Array
                                (
                                    [id_option] => 4
                                    [qty_option] => 1
                                )
                            [70617_option] => Array
                                (
                                    [id_option] => 5
                                    [qty_option] => 1
                                )
                        )
                )
            [86953_cart] => Array
                (
                    [id_agenda] => 17
                    [qty_agenda] => 1
                )
        )
)

The Question is how I can delete or unset this cookies from parent till child level of the array (let's say I want to delete agenda with key : 90406_cart or 52058_option )? 问题是如何从父级删除或取消设置此cookie,直到阵列的子级(假设我要删除键为90406_cart52058_option议程)? Cookies was set using this script build on Codeigniter Cookies是在Codeigniter上使用此脚本构建的

$id_agenda = 1;

$qty_agenda = 20;

$option = array(1,2,3);

$qty_option = array(3,3,3);

$num = rand(10000,99999).'_cart';

$this->input->set_cookie('cart['.$num.'][id_agenda]',$id_agenda, 86400); 
$this->input->set_cookie('cart['.$num.'][qty_agenda]',$qty_agenda, 86400); 
for($i = 0; $i<count($id_option);$i++){     
    $num2 = rand(10000,99999).'_option';
    $this->input->set_cookie('cart['.$num.'][options]['.$num2.'][id_option]',$id_option[$i], 86400);        
    $this->input->set_cookie('cart['.$num.'][options]['.$num2.'][qty_option]',$qty_option[$i], 86400);      
}

So after experiment a bit using web development tools in chrome, I just realize that cookies was store in a row like : 因此,在使用chrome浏览器中的Web开发工具进行了一些实验之后,我才意识到Cookie的存储位置如下:

cart[34705_cart][id_agenda] = 43
cart[34705_cart][qty_agenda] = 2
cart[34705_cart][options][12263_option][id_option] = 2
cart[34705_cart][options][12263_option][qty_option] = 4

Which mean it is stored in one row for each data and then PHP read it all as one variable with type of an Array which is practically impossible to delete / unset cookies just by using the array key like unset($_COOKIES['cart']['90406_cart']) or $this->input->set_cookie('34705_cart') . 这意味着它将每个数据存储在一行中,然后PHP将其作为一个Array类型的变量全部读取为一个Array ,实际上仅使用unset($_COOKIES['cart']['90406_cart'])$this->input->set_cookie('34705_cart') The solution I came up is like this: 我提出的解决方案是这样的:

// delete all cookies for agenda and including any options on the agenda array
$array_key = '34705_cart';
$this->input->set_cookie('cart['.$array_key.'][id_agenda]'); 
$this->input->set_cookie('cart['.$array_key.'][qty_agenda]'); 
foreach($data[$array_key]['options'] as $key => $item){
    $this->input->set_cookie('cart['.$array_key.'][options]['.$key.'][id_option]'); 
    $this->input->set_cookie('cart['.$array_key.'][options]['.$key.'][qty_option]'); 
}

// delete option on the agenda
$array_key = '34705_cart';
$array_key2 = '12263_option';

$data = $this->input->cookie('cart');

$this->input->set_cookie('cart['.$array_key.'][options]['.$array_key2.'][id_option]');
$this->input->set_cookie('cart['.$array_key.'][options]['.$array_key2.'][qty_option]');
if(count($data[$array_key]['options'][$array_key2]) == 1){
    $this->input->set_cookie('cart['.$array_key.'][options]');
}

I hope this help someone who maybe tumbling on case like this one. 我希望这可以帮助可能在这种情况下翻滚的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM