Create 2 private functions i.e ( _sessionSet() and _sessionClear() )
$this->session->set('profile', array(
'firstname' => $newvalue['firstname'],
'surname' => $newvalue['surname'],
));
}
private function _sessionClear($newvalue = null){
$this->session->set('profile', array(
'firstname' => '',
'surname' => '',
));
}
You will call _sessionSet(){ //your session set here} when setting session variables for the very first time.
$profile = ['firstname' => "bill", 'surname' => "gates"];
$this->_sessionSet($profile);
When the profile value changes you will call _sessionClear() function which will clear the session variables and then call the session set function
$profile = $this->session->get('profile');
$profile['surname'] = "williams";
//clear the session values
$this->sessionClear()
//set new values
$this->sessionSet($profile);
Using control structures and conditional statements inside the above private functions I hope you can use the above example to achieve what you intend and maybe just work with one private function.