[英]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 我正在学习Yii,并且我正在尝试使用此操作在会话中保存数组
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 它完美地工作。var_dump(Yii :: app()-> session ['_ products'])的结果是
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. 现在,我想从会话中存储的数组中删除值“ Tata” 。 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? 但是会引发错误* 间接修改CHttpSession的重载元素没有任何作用*所以我的问题是如何从会话中存储的数组中删除一个值?
$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
然后我从该数组中删除了值,然后再次将会话的值设置为array.this是代码
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';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.