Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法在Yii2中取消设置会话变量_Php_Session_Yii_Yii2_Unset - Fatal编程技术网

Php 无法在Yii2中取消设置会话变量

Php 无法在Yii2中取消设置会话变量,php,session,yii,yii2,unset,Php,Session,Yii,Yii2,Unset,我正在使用Yii2,而且我刚刚开始在其中使用会话。我已经在Yii网站上为他们阅读了这篇文章 我注意到的一点是,如果不使用标准的superglobal$\u会话,在会话中使用多维数组有点困难,因此我主要使用它 但有一件事我很难做到,那就是取消设置会话变量 例如: if (!Yii::$app->session->isActive) { Yii::$app->session->open(); } print_r($_SESSION['foo']); if ($th

我正在使用
Yii2
,而且我刚刚开始在其中使用
会话。我已经在Yii网站上为他们阅读了这篇文章

我注意到的一点是,如果不使用标准的superglobal
$\u会话
,在会话中使用多维数组有点困难,因此我主要使用它

但有一件事我很难做到,那就是取消设置会话变量

例如:

if (!Yii::$app->session->isActive) {
    Yii::$app->session->open();
}

print_r($_SESSION['foo']);

if ($this->command == 'sample_action') {

    if (!isset($_SESSION['foo'][$this->some_id][$this->example_id])) {
        $_SESSION['foo'][$this->some_id][$this->example_id] = $this->example_id;
        $result = true;
    }

} elseif ($this->command == 'sample_action_2') {

    if (isset($_SESSION['foo'][$this->some_id][$this->example_id])) {
        unset($_SESSION['foo'][$this->some_id][$this->example_id]);
        //$_SESSION['foo'][$this->some_id][$this->example_id] = ''; // This works
        $result = true;
    }           

}

print_r($_SESSION['foo']);
对它使用
unset
根本不起作用,它仍然存在。但是,将其设置为空值是可行的。

试试这个

$session = Yii::$app->session;
$session->remove('foo');

可能对您有所帮助。

终于找到了一个有效的解决方案,希望这对其他人有所帮助:

$session = Yii::$app->session;

$foo = $session['foo'];

if ($this->command == 'sample_action') {
    $foo[$this->some_id][$this->example_id] = $this->example_id;
} elseif ($this->command == 'sample_action_2') {
    unset($foo[$this->some_id][$this->example_id]);
}

$session['foo'] = $foo;

是的,我知道你可以这样做,但是如何删除多维数组中的值呢?这将只删除整个
$\u会话['foo']
<代码>删除
只能在第一级删除。