Session Array Update in codeigniter

让人想犯罪 __ 提交于 2020-01-05 07:43:11

问题


I have to do following tasks.

1) Take an array and save it into the seesion. At start it is empty error and i am doing this

    $id_array=array();
    $this->session->set_userdata('PID', $id_array);

2) After that i take some value from the user and then go to the controller ..Take an array from the session.which was initially empty .I insert that user value into array and then again insert that array into session. i am doing it like this...

 $username['name']=$this->session->userdata['PID'];
 array_push($username,$PID);//this $PID is variable which i m getting from user
 $this->session->set_userdata('PID', $username);

So user repeat this process two and three time. SO thats mean 3 value has been inserted into session into different index.But when at last i take data from session and print it..These values are there but the index is same...But according to my reuqirement index should be differnt.i am print it like that

 $username['name']= $this->session->userdata('PID');
 print_r($username);

I have entered 6 in three time.6 should be appear three time on 0 1 2 index but 6 appear 3 time but on the same index like this.

Array ( [name] => Array ( [name] => Array ( [name] => Array ( [name] => Array ( [name] => Array ( ) [0] => 6 ) [0] => 6 ) [0] => 6 ) [0] => 6 ) )

I dont know what is the problem.


回答1:


Try this.

 $username=$this->session->userdata['PID'];  // read the session
 array_push($username,$PID);//this $PID is variable which i m getting from user
 $this->session->set_userdata('PID', $username);                               

This will remove the 'name' index from array.



来源:https://stackoverflow.com/questions/14313975/session-array-update-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!