Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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
Session 在CodeIgniter中使用会话库_Session_Codeigniter - Fatal编程技术网

Session 在CodeIgniter中使用会话库

Session 在CodeIgniter中使用会话库,session,codeigniter,Session,Codeigniter,由于某些原因,我很难理解CI中的会话是如何工作的。 我想将下面的代码部分改为使用CodeIgniter会话,而不是PHP中通常的方式。最好的方法是什么 foreach($_POST['qty'] as $k => $v) { $id = (int)$k; $qty = (int)$v; $_SESSION['cart'][$id]['quantity'] = $qty; } 还有一个问题 在使用CI会话库时,当会话具有多维结构时,在读取所需的值之前,是否总是必须

由于某些原因,我很难理解CI中的会话是如何工作的。
我想将下面的代码部分改为使用CodeIgniter会话,而不是PHP中通常的方式。最好的方法是什么

foreach($_POST['qty'] as $k => $v) {
    $id = (int)$k;
    $qty = (int)$v;

    $_SESSION['cart'][$id]['quantity'] = $qty;
}
还有一个问题
在使用CI会话库时,当会话具有多维结构时,在读取所需的值之前,是否总是必须先将会话的内容放到数组中?

我会这样做

// Fetch the cart from the session
$cart = $this->session->userdata('cart');

// Update the cart
foreach ($this->input->post('qty') as $k => $v)
{
    $id = (int) $k;
    $qty = (int) $v;

    $cart[$id]['quantity'] = $qty;
}

// Save the cart back to the session
$this->session->set_userdata('cart', $cart);

谢谢这看起来很合理,我要试一试。