在codeigniter中存储数组中的值

时间:2016-06-16 04:44:02

标签: arrays codeigniter-3

我遇到了一个问题,我希望将数组中的ID存储在一个会话中,如数组([' docid'] => 1,[' docid&# 39] =→2,['文档ID&#39] =→3,['文档ID&#39] =→4,['文档ID'] =&GT ; 5,[' docid'] => 6,[' docid'] => 7)就像这样,当我点击时,我希望单个会话中的ID在链接上,任何帮助将不胜感激。谢谢

这是我的代码: -

public function addtoshortlist()
{

     $shortlist=array('docid'=>$_GET['id']);
    $this->session->set_userdata('shortlist',$shortlist);
    print_r($this->session->userdata('shortlist'));
    //$this->session->userdata('shortlist')['docid'];

    redirect($this->agent->referrer());
}

1 个答案:

答案 0 :(得分:0)

试试这个

public function addtoshortlist()
{
     $shortlist=array('docid' => array($_GET['id']));
     if(!$this->session->userdata('shortlist')['docid'])
     {
          $this->session->set_userdata('shortlist',$shortlist);
     }else{
          $session = $this->session->userdata('shortlist')['docid'];
          if(!in_array($_GET['id'],$session))
          $session[]=$_GET['id'];
          $this->session->set_userdata('shortlist',array('docid'=>$session));
     }
     print_r($this->session->userdata('shortlist'));

    redirect($this->agent->referrer());
}