CodeIgniter Forums
Updating a session array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Updating a session array (/showthread.php?tid=71969)



Updating a session array - Qodi - 10-19-2018

I would like to update a session array using the codeigniter session library terminology.

I have a session array named chairs. Each key of the array is the chairID of the associated chair and the value of the chairID key is the quantity of chairs of with that ID.


I am looking at how I can update the array to add chairs to the array. 


I know the php syntax (bold). Below it is a list of possible codeigniter session library approaches I've postulated. They are probably all incorrect, I would appreciate some guidance.


$_SESSION['chairs'][$chairID] = $chairQuant;
               
$this->sesison->chairs[$chairID] = $chairQuant; (Simply replacing $_SESSION['chairs'] with $this->sesison->chairs)
               
$this->session->chairs += [$chairID => $chairQuant] (using unary array union operator)
                
{


$array = $this->session->chairs; 
               
array_push($array, array($chairID => $chairQuant))
               
$this->session->set_userdata(chairs, $array);


}  (Seemingly the most terminologically faithful approach, assign the session array value to another array, update this array using the array_push, then set the session array with the value of this new array. This approach feels very long winded, considering the alternative(s).)

Would appreciate help!

Thanks

God bless.


RE: Updating a session array - Sunchock - 01-31-2025

Hello Igniters,

Sorry to bring up such an old topic, I'm having the exact same problem.

I was wondering how to handle multi-level session variables using CodeIgniter?

For now, I'm using:
PHP Code:
// Initialization
session()->push('app1', ['subA' => []]);
// Creating a variable into app1/subA 
session('app1')['subA']['var01'] = "Such text"

But if I want to check elsewhere in the code if the var01 exists recursively, how do I achieve this?

PHP Code:
// For now, I use:
if (isset(session('app1') && isset(session('app1')['subA']) && isset(session('app1')['subA']['var01'])) 

// I would expect something like :
session('app1')->has('subA.var01');
// or
session()->has('app1.subA.var01'); 

Same problem if I want to delete subA but not others variables into app1 ?

PHP Code:
// For now, I use:
unset($_SESSION['app1']['subA']);

// I would expect something like :
session('app1')->remove('subA');
// or
session()->remove('app1.subA'); 


So I asked myself: am I doing things wrong?
I mean: what are the best practices for handling session subvariables?


RE: Updating a session array - luckmoshy - 01-31-2025

PHP Code:
$session->push('hobbies', ['sport' => 'tennis']); 

PHP Code:
<?php

$newdata 
= [
    
'username'  => 'johndoe',
    
'email'     => '[email protected]',
    
'logged_in' => true,
];

$session->set($newdata); 

https://codeigniter.com/user_guide/libraries/sessions.html


RE: Updating a session array - InsiteFX - 01-31-2025

Pushing New Value to Session Data.

The push() method is used to push a new value onto a session value that is an array.
For instance, if the hobbies key contains an array of hobbies, you can add a new value
onto the array like so:

PHP Code:
<?php

$session
->push('hobbies', ['sport' => 'tennis']); 



RE: Updating a session array - Sunchock - 02-01-2025

Sorry if my post wasn't explicit enough.

I have no problem creating multi-level session variables.

Unless I'm mistaken, I have the impression that there is no way to modify and/or delete variables at level 2 or higher using CodeIgniter. So I fell back on PHP's native methods (unset, isset, etc.).

I would just like to know what are the best practices for MODIFYing or DELETING variables at a level 2 or 3, etc. without impacting the other variables.

I have already read and reread the doc several times before posting here


RE: Updating a session array - ozornick - 02-04-2025

No way. Inside session(), it's just checking isset($_SESSION[$key]), so you check the primary key in has() get()...
You can save the array to a variable and already work with it, and overwrite the session at the end.
It's like working with SQL - get row data , processing, saving.