Codeigniter会话的数组操作

时间:2011-01-12 06:54:52

标签: php arrays codeigniter

继续本主题link text

你好。我正在使用codeigniter并希望在会话中存储数据。我有这个数组

array(4) { 
[0]=> array(2) { ["DESC"]=> string(1) "A" ["SUBFUNCTION_ID"]=> string(3) "904" } 
[1]=> array(2) { ["DESC"]=> string(1) "B" ["SUBFUNCTION_ID"]=> string(3) "903" } 
[2]=> array(2) { ["DESC"]=> string(1) "C" ["SUBFUNCTION_ID"]=> string(3) "902" } 
[3]=> array(2) { ["DESC"]=> string(1) "D" ["SUBFUNCTION_ID"]=> string(3) "901" } 
} 

这是存储会话数据的数组

$data = array(
'username' => $this->input->post('username'),
'userid' => $query->USER_ID,    
'role' => $query->ROLE_ID,    
'is_logged_in' => true

);

如何操作第一个数组然后将数据附加到第二个数组?我希望它成为像这样的东西

$data = array(
'username' => $this->input->post('username'),
'userid' => $query->USER_ID,    
'role' => $query->ROLE_ID,    
'is_logged_in' => true,
'A' => 904,
'B' => 903,
'C' => 902,
'D' => 901

);

提前致谢

3 个答案:

答案 0 :(得分:2)

获取数组并对其进行操作。

$user_array = $this->session->userdata('your_user_data');

foreach ($your_array as $sub_array)
{
    $user_array[$sub_array['DESC']] = $sub_array['SUBFUNCTION_ID'];
}

$this->session->set_userdata('your_user_data', $user_array);

你会有这样的事情:

$ data = $ this-> session-> userdata('your_user_data');

$data = array(
'username' => $this->input->post('username'),
'userid' => $query->USER_ID,    
'role' => $query->ROLE_ID,    
'is_logged_in' => true,
'A' => 904,
'B' => 903,
'C' => 902,
'D' => 901
)

答案 1 :(得分:1)

如果密钥不同,您只需添加数组:$arr = $arr1 + $arr2;

答案 2 :(得分:0)

听起来您的会话数据格式为$foo并且起始数据为$data

$foo = array (
    array('DESC'=>'A', 'SUBFUNCTION_ID'=>904),
    array('DESC'=>'B', 'SUBFUNCTION_ID'=>903),
    array('DESC'=>'C', 'SUBFUNCTION_ID'=>902),
    array('DESC'=>'D', 'SUBFUNCTION_ID'=>901),
);

$data = array(
    'username' => $this->input->post('username'),
    'userid' => $query->USER_ID,    
    'role' => $query->ROLE_ID,    
    'is_logged_in' => true
);

要根据您的要求将$foo中的数据合并到$data,请循环显示$foo中的所有值,然后将其附加到$data

foreach($foo as $bar) {
    $data[$bar['DESC']] = $bar['SUBFUNCTION_ID'];
}