0

I want to add the array value in Session. But it overwrite instead of adding new value.

This is the process of the function.

 public function example(){
    $fileName = $file . "_demo_a.htm";
    $demo["filename"][] = $fileName;
    Session::put($demo);
}

For example , when I run the functino for $file = a , the value is added in Session.But for the next time like $file = b , the value is overwritten.

The result what I want is like this ,

array (
  'filename' => 
  array (
    0 => 'a_demo.htm',
    1 => 'b_demo.htm'
  ),
)  
3
  • See Pushing To Array Session Values. You should use push() instead. Commented Jan 22, 2019 at 7:44
  • Thank guys! I got my result after trying Session::push() Commented Jan 22, 2019 at 7:46
  • Quick tip, only use session to store data with page to page requests. If using the session with API development then look for alternative ways to persistent data as sessions will suffer from final write issues when performing multiple concurrent connections for the same session. This means that if you fire multiple requests all at the same time and they all alter some aspect of the session ( can be anything ) then the last connection to complete will trump any changes made by the other sessions due to a session performing a PUT to write back to the session manager and not a PATCH mechanism. Commented Jan 22, 2019 at 7:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.