简体   繁体   中英

Delete cookie using array key in PHP

Just wanna know how to delete cookies on PHP. below are printed result of $_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.

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 )? Cookies was set using this script build on 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 :

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') . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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