我似乎被困在这个问题上安静了几次,基本上我在codeigniter中使用了cookie,并将不同名称的数组传递给不同的函数,设置cookie的代码是
$data = array (
'client_block_ID' => $client_block_ID,
'client_unit_ID' => $client_unit_ID,
'blockUnits' => $blockUnits
);
$cookieName ='tab'.$counter;
$cookie = array(
'name' => $cookieName,
'value' => $data,
'expire' => '86500',
);
$this->input->set_cookie($cookie);
现在我只是不知道如何获取数组中的变量,即获取client_block_ID的语法是什么?
答案 0 :(得分:3)
现在你的cookie数组看起来像这样.........
$cookie = array(
'name' => $cookieName,
'value' => array(
'client_block_ID' => $client_block_ID,
'client_unit_ID' => $client_unit_ID,
'blockUnits' => $blockUnits
);
'expire' => '86500',
);
因此,要从$ cookie数组中获取client_block_ID,您必须循环遍历该数组,如下所示。
foreach($cookie as $c)
{
echo $c['name'];
foreach($c['value'] as $v)
{
echo $v['client_block_ID'];
}
}
答案 1 :(得分:1)
你没想到数组是值; 我做了,我明白了:
A PHP Error was encountered
Severity: Warning
Message: setcookie() expects parameter 2 to be string, array given
Filename: core/Input.php
Line Number: 404
答案 2 :(得分:0)
使用
$this->input->cookie()
让您获取Cookie。第一个参数将包含您要查找的cookie的名称(包括任何前缀)
如果您尝试检索的项目不存在,则该函数返回FALSE(布尔值)。
试试这个,
$cookievalue= $this->input->cookie('value');
if($cookievalue){
//cookie exists
foreach($cookievalue as $cookie){
echo $cookie['client_block_ID'];
}
}else{
//cookie doesnot exists
}