简体   繁体   中英

how to delete a value from array in cookie array?

I am using Cakephp 3.0.I am new in cookie concept. I have successfully created a cookie but i want to delete a value from cookie array. Here is my code:-

enter code here
$cookiedata=
Array
(
[0] => 2
[1] => 1
)

$_GET['id'] = 2;

public function cartitems(){
    $cookiedata = $this->Cookie->read('ProductCartdata'); 
    $this->loadModel('Products');
    $query = $this->Products->find('all')->where(['id IN' =>$cookiedata]);
    $products = $query->hydrate(false)->toArray();
        if(isset($_GET['id'])){
            if(($key = array_search($_GET['id'], $cookiedata)) !== false) {
                    unset($cookiedata[$key]);
            } 
    $this->redirect(['controller'=>'test','action' => 'cartitems']);    
   }
        $this->set(compact('products','cookiedata'));
}

And i want that result:-

enter code here
Array
(
[0]=>1
)

You can read up on the Cookie-component in the documentation. There is a delete-method if you completely want to remove the data in your cookie:

Cookie::delete($key)

In your case it probably makes more sense to write over the existing data after looping:

$this->Cookie->write('ProductCartdata', $cookiedata);

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