cyberfly
cyberfly

Reputation: 5848

Array Manipulation of Codeigniter's Sessions

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

Upvotes: 2

Views: 1936

Answers (3)

Teej
Teej

Reputation: 12873

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
)

Upvotes: 2

Josh Schumacher
Josh Schumacher

Reputation: 198

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'];
}

Upvotes: 0

StasM
StasM

Reputation: 11002

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

Upvotes: 1

Related Questions