简体   繁体   中英

Removing a value from array stored in session

I am learning Yii and i am trying to save an array in session using this action

public function actionStoreProducts($name)
        {
            $name=trim(strip_tags($name));
            $session = new CHttpSession;               //add this line
             $session->open(); 
            if(!empty($name))
            {
                if(!isset(Yii::app()->session['_products']))
                {
                    $session->add('_products', array($name)); 
                    $this->redirect(Yii::app()->request->urlReferrer);
            }
            else
            {

                $myProducts = Yii::app()->session['_products'];
                foreach(Yii::app()->session['_products'] as $value)
                {
                    if($value===$name)
                    {
                        $this->redirect(Yii::app()->request->urlReferrer);
                        Yii::app()->end();
                    }
                }
                $myProducts[] = $name;
               $session->add('_products', $myProducts);
               $session->close();
                $this->redirect(Yii::app()->request->urlReferrer);

            }

        }

Its working perfectly.The result of var_dump(Yii::app()->session['_products']) is

array(2) { [0] => string(5) "birla" [1] => string(4) "Tata" } 

Now i want to delete the value "Tata" from this array stored in the session. I tried this

public function actionCheck()
        {
           $name= 'Tata';
           if(isset(Yii::app()->session['_products']))
           {
               $session=new CHttpSession;
               $session->open();
               if(in_array($name,Yii::app()->session['_products'] ))
                {
                    $keyIs=  array_keys(Yii::app()->session['_products'], $name);
                    unset(Yii::app()->session['_products'][$keyIs[0]]);


                }

           }
           else
           {
               echo 'session is not set';
           }

        } 

but it throws the error * Indirect modification of overloaded element of CHttpSession has no effect * so my question is how can i delete a value from the array stored in the session?

$name= 'Tata';
    if(isset(Yii::app()->session['_products']))
    {
        $k=array_search($name,Yii::app()->session['_products']);
        if ($k!==false){
            unset(Yii::app()->session['_products'][$k]);
        }
    }

Same unset as in simple array.

I have solved the problem.i just passed the array from the session to a variable. and then i deleted the value from that array and then again set the value of the session as the array.this is the code

public function actionCheck()
{
$name='Tata';
            if(isset(Yii::app()->session['_products']))
            {

                $arrayOfProducts=Yii::app()->session['_products'];
                $keyIs=  array_search($name, $arrayOfProducts);
                if($keyIs!== false)
                {
                    unset($arrayOfProducts[$keyIs]);
                    Yii::app()->session['_products']=$arrayOfProducts;
                }
           }

}

As I said earlier to you, session property is readonly.

for removing a session you must do this :

public function actionCheck()
        {
           $name= 'Tata';
           if(isset(Yii::app()->session['_products']))
           {
               $session=new CHttpSession;
               $session->open();
               if(in_array($name,Yii::app()->session['_products'] ))
                {
                    $keyIs=  array_keys(Yii::app()->session['_products'], $name);
                    $session->remove($name);     //Replace this line whit unset


                }

           }
           else
           {
               echo 'session is not set';
           }

        }

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