2

Continue from this topic link text

Hi guys. I am using codeigniter and want to store data in the session. I have this array

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

And this is the array to store the session data

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

);

How can i manipulate the first array and then append the data into the second array? I want it to become something like this

$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

);

Thanks in advance

3 Answers 3

2

Get the array and manipulate it.

$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);

You would have something like this:

$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
)
Sign up to request clarification or add additional context in comments.

Comments

1

If the keys are different, you can just add arrays: $arr = $arr1 + $arr2;

Comments

0

It sounds like you have your session data in the form of $foo and starting data $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
);

To merge in the data from $foo into $data as you requested, loop over all values in $foo and then append them to $data

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.