在编码器中无法从cookie中获取关联数组变量


cant get associative array variables from cookies in 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的语法是什么??

现在你的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'];
    }
}

您没有期望数组作为值;我做了,我看到:

A PHP Error was encountered
Severity: Warning
Message: setcookie() expects parameter 2 to be string, array given
Filename: core/Input.php
Line Number: 404

use

$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
}